Search for a struct item in a vector by member data

后端 未结 4 1552
后悔当初
后悔当初 2020-11-29 03:36

I\'m very new to c++ and I\'m trying to find a way to search a vector of structs for a struct with a certain member data.

I know this would work with simple types in

4条回答
  •  暖寄归人
    2020-11-29 04:11

    You can use std::find_if in combination with functors (if you are working with C++98) or lambdas (if you are using C++11, which I will assume):

    using namespace std;
    int ID = 3; // Let's say...
    auto it = find_if(begin(vector), end(vector), [=] (Friend const& f) { 
        return (f.ID == ID); 
        });
    bool found = (it != end(vector));
    

提交回复
热议问题