c 2b 2b find object in vector by attribute

Solutions on MaxInterview for c 2b 2b find object in vector by attribute by the best coders in the world

showing results for - "c 2b 2b find object in vector by attribute"
Ninon
22 Feb 2018
1std::vector<Type> v = ....;
2std::string myString = ....;
3auto it = find_if(v.begin(), v.end(), [&myString](const Type& obj) {return obj.getName() == myString;})
4
5if (it != v.end())
6{
7  // found element. it is an iterator to the first matching element.
8  // if you really need the index, you can also get it:
9  auto index = std::distance(v.begin(), it);
10}