Search
 
SCRIPT & CODE EXAMPLE
 

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);
}
Comment

find a member variable in a vector of objects 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_;
};
Comment

PREVIOUS NEXT
Code Example
Cpp :: std::random_device 
Cpp :: c++ void to avoid functions 
Cpp :: convert c++ to python online 
Cpp :: c++ linker input and output 
Cpp :: beecrowd problem 1004 solution 
Cpp :: how to make a running text in c++ 
Cpp :: Hiring Test codechef solution in c++ 
Cpp :: how to make a substring after certain position 
Cpp :: Problems in your to-do list codechef solution in c++ 
Cpp :: librerias matematicas en c++ para numeros aleatorios 
Cpp :: c++ calorie calculator using a for loop 
Cpp :: 12 to december in c++ code 
Cpp :: calculate number of edges of graph in data structure c++ 
Cpp :: lambda - print-out array and add comment 
Cpp :: c++ multiplication table rows and columns 
Cpp :: Processing a string- CodeChef Solution in CPP 
Cpp :: c++ sort a 2d vector by column 
Cpp :: beecrowd problem 1001 solution 
Cpp :: c++ max 
Cpp :: c++ function parameters 
Cpp :: constants in cpp 
Cpp :: while loop c++ 
Cpp :: c++ press any key 
Cpp :: nazi crosshair c++ 
C :: c bold text 
C :: print an array in c 
C :: how to map one value to another in C 
C :: get chunks of a mp4 in ffmpeg 
C :: arduino digital read 
C :: clrscr in c 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =