“Encontre uma variável de membro em um vetor de objetos CPP” Respostas de código

Encontre uma variável de membro em um vetor de objetos CPP

//Using a standard functor

struct MatchString
{
 MatchString(const std::string& s) : s_(s) {}
 bool operator()(const Type& obj) const
 {
   return obj.getName() == s_;
 }
 private:
   const std::string& s_;
};
UBoiii

Encontre uma variável de membro em um vetor de objetos 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);
}
UBoiii

Respostas semelhantes a “Encontre uma variável de membro em um vetor de objetos CPP”

Perguntas semelhantes a “Encontre uma variável de membro em um vetor de objetos CPP”

Procure respostas de código populares por idioma

Procurar outros idiomas de código