Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

find a member variable in a vector of objects cpp

//Using a lambda function (only C++11 or newer)
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);
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #find #member #variable #vector #objects #cpp
ADD COMMENT
Topic
Name
2+8 =