How to check if a value is included in a list

I have a list of l like list<pair<int,int>>. How to check if x pair<int,int> x=make_pair(5,6)is in list l?

+5
source share
3 answers

Use std::find:

std::find(l.begin(), l.end(), x) != l.end()
+18
source

Use std::find:

auto it = std::find(lst.begin(), lst.end(), x);
if ( it != lst.end() )
{
   //x found
}
+4
source

Use the algorithm std::find():

std::list<std::pair<int, int>> my_list;
my_list.push_back(std::make_pair(1, 2));
my_list.push_back(std::make_pair(3, 2));

auto i = std::find(my_list.begin(), my_list.end(), std::make_pair(3, 2));
if (i != my_list.end())
{
    // Found it.
}
+3
source

All Articles