How to check if value is in list

13,625

Solution 1

Use std::find:

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

Solution 2

Use std::find:

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

Solution 3

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.
}
Share:
13,625
Damir
Author by

Damir

Updated on June 08, 2022

Comments

  • Damir
    Damir almost 2 years

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