Answers for "c++ find object in vector by attribute"

C++
0

c++ find object in vector by attribute

std::vector<Type> v = ....;
std::string myString = ....;
auto it = find_if(v.begin(), v.end(), [&myString](const Type& obj) {return obj.getName() == myString;})

if (it != v.end())
{
  // found element. it is an iterator to the first matching element.
  // if you really need the index, you can also get it:
  auto index = std::distance(v.begin(), it);
}
Posted by: Guest on May-02-2020

Code answers related to "c++ find object in vector by attribute"

Browse Popular Code Answers by Language