Share via

C++ vector - find elements inside a vector and create a new vector

Markus Freitag 3,791 Reputation points
2022-10-14T04:47:53.687+00:00

Hello,
Unfortunately, there is still a problem.
https://learn.microsoft.com/en-us/answers/questions/1031079/c-vector-find-element.html

struct PanelData  
{  
	 // **   
	 CString Price;  
	 CString IdentNo;  
  
	 bool    PanelReported;  
  
};  
  
vector<PanelData>      m_dequePanelData;  
vector<PanelData>      m_Result;  
  

Is there a way that returns me a new vector list, here all values of the PanelReported variable is true.

std::vector<PanelData>::iterator it;  
 it = find(m_dequePanelData.begin(), m_dequePanelData.end(),  []( const PanelData& d ) { return d.PanelReported; }  );	   
 //std::vector<PanelData>::iterator it2;  
 //auto it2 = find(m_dequePanelData.begin(), m_dequePanelData.end(), [](const PanelData& d) { return d.PanelReported; });  
	  
//In C# I have FirstOrDefault() or ToList()  I am looking for similar.  
//What is the correct name in C++, what functions are similar?	   
  
The simplest solution just go through the list, think a std function is faster, better.  
for (auto it = m_DicProcessdata.begin(); it != m_DicProcessdata.end(); ++it)  
{	   

Thanks!

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.