C++ vector - find element

Markus Freitag 3,791 Reputation points
2022-09-30T17:23:34.337+00:00

Hello,

struct PanelData  
	{  
		// **   
		CString Price;  
		CString IdentNo;  
  
		bool    PanelReported;  
	  
	};  
	  
	vector<PanelData>	  m_dequePanelData;  
	  
  
std::vector<PanelData>::iterator it;  
it = find(m_dequePanelData.begin(), m_dequePanelData.end(), it->PanelReported = true);  
  
int pos = it - m_dequePanelData.begin();  

I am using a vector and want to find the PanelData,
where the first variable PanelReported is true.

I use a vector and want to find the panel data where the variables PanelReported are true.

How can I achieve this?

  • So once targeted the first hit like FirstOrDefault()
  • Furthermore a list with the value true.
    246458-ex-it-05.png

Current error message.
Why and how can I solve it? Thanks.

Developer technologies C++
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2022-09-30T17:47:33.497+00:00

    Try this code:

    it = std::find_if( m_dequePanelData.begin( ), m_dequePanelData.end( ), []( const PanelData& d ) { return d.PanelReported; } );  
    if( it == m_dequePanelData.end( ) )  
    {  
        // not found  
        // . . .  
    }  
    else  
    {  
        // found  
        int pos = it - m_dequePanelData.begin( );  
        // . . .  
    }  
    

    If you want to examine all of items, you can use a loop instead of find_if.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

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