C++ vector - find element, find_if, copy_if

Markus Freitag 3,791 Reputation points
2022-10-18T08:03:22.157+00:00

Hello,

it is clear. Struct all is public!

struct PanelData  
{  
	CString Error;  
	CString OrderNo;  
	double SellingPrice;  
  
	map<CString, Component> m_DicProcessdata;  
  
	CString BoardId;  
	bool    PanelReported;  
	  
	vector<subBoard> VecSubBoards;  
	PanelData(CString strBoardId);  
  
	bool operator==(bool a)  
	{  
		return PanelReported == a;  
	}  
};  

https://cplusplus.com/reference/algorithm/find_if
https://cplusplus.com/reference/algorithm/copy_if/

Is predicate, true or false.
How would I have to do it if I say Price less than or equal to 30,34€

For whatever reason is not clear.
When does it make sense?
What would I have to call the call?

https://cplusplus.com/reference/algorithm/find/

The examples only use int, no class, no structure, which is easier.

it9 = find(m_dequePanelData.begin(), m_dequePanelData.end(), [](const PanelData& d) { if (d.Price >= 3) return d; });  
	/*if (it9 != m_dequePanelData.end())  
		std::cout << "Element found in m_dequePanelData: " << *it9->Price << '\n';  
	else  
		std::cout << "Element not found in m_dequePanelData\n";*/  

The third parameter must be the same. I use PanalData as vector now, not bool. Does not work. return value is d from PanelData.

What is the best way to proceed when looking at the transfer parameters?

251531--find-only-a-value-bool-int-or-other-3.png

251497--copy-if-predicate-bool-01.png

251498--find-if-predicate-bool-01.png

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.
0 comments No comments
{count} votes

Answer accepted by question author
  1. WayneAKing 4,931 Reputation points
    2022-10-19T00:15:17.67+00:00

    I haven't followed all of your code and issues, but if your
    basic question is how to check a vector of structs to find
    an element that meets a condition in one of the struct
    members - using std::find_if with a predicate:

    // find the first struct in a vector with a double   
    // member <= a given value  
      
    #include <iostream>     // std::cout  
    #include <algorithm>    // std::find_if  
    #include <vector>       // std::vector  
    #include <iomanip>  
      
    struct MyStruct  
    {  
        double price;  
    };  
      
    double threshold = 0.0;  
    bool PriceRanges(MyStruct ms)  
    {  
        return (ms.price <= threshold);  
    }  
      
    int main() {      
        std::vector<MyStruct> myvector;  
      
        MyStruct mystruxt;  
           
        mystruxt.price = 35.00;  
        myvector.push_back(mystruxt);  
      
        mystruxt.price = 41.00;  
        myvector.push_back(mystruxt);  
      
        mystruxt.price = 22.50;  
        myvector.push_back(mystruxt);  
      
        mystruxt.price = 11.00;  
        myvector.push_back(mystruxt);  
      
        threshold = 30.34;  
        //threshold = 10.00;  
        std::vector<MyStruct>::iterator it =   
            std::find_if(myvector.begin(), myvector.end(), PriceRanges);  
      
        if (it != myvector.end())  
        {  
            std::cout << std::fixed;  
            std::cout << std::setprecision(2);  
            std::cout << "The first value <= threshold of "  
                << threshold << " is " << it->price << '\n';  
        }  
        else   
        {  
            std::cout << "No struct found in vector with a Price <= "  
                << threshold << std::endl;  
        }  
      
        return 0;  
    }  
      
      
    

    Obviously, a lambda could be used instead of a predicate function
    for those so-inclined.

    • Wayne
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. WayneAKing 4,931 Reputation points
    2022-10-20T08:17:51.103+00:00

    but now we have here also an array [=]

    There is no "array" involved in that code or any of the
    other that has been posted in this thread. The symbols
    [] and [=] in the code posted is the first part of a
    lambda, known as the "capture clause (Also known as the
    lambda-introducer in the C++ specification.)" See
    the docs for a description of all of the parts of a
    lambda expression:

    Lambda expressions in C++
    https://learn.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp?view=msvc-170

    • Wayne
    1 person found this answer helpful.

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.