Reputation: 23
I'm very new to c++ and I'm trying to figure how to find a struct inside a list using a string.
I have a struct like this:
struct entrada {
string token;
string lexema;
string tipo;
};
and a list:
list<entrada> simbolos;
Insert here some 'entrada' in 'simbolos'
Let's say I want to search for a 'entrada' with a certain 'lexema', and cout the other strings. Is there a simple way to do this? Like a function or something. I did it using while/for, but it isn't how I want to do.
Upvotes: 2
Views: 3373
Reputation: 9715
In accordance with your comments, the following snippet shows you a simple way to search an element into a container using the algorithm in the STL std::find_if
.
auto match = std::find_if(simbols.cbegin(), simbols.cend(), [] (const entrada& s) {
return s.lexema == "2";
});
if (match != simbols.cend()) {
std::cout << match->token << '\n'
<< match->lexema << '\n'
<< match->tipo << '\n';
}
At least C++11 is required.
Upvotes: 3