Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if value is in list

Tags:

c++

stl

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 ?

like image 251
Damir Avatar asked Jul 11 '12 09:07

Damir


People also ask

How do I check if a value is in a list Python?

We can use the in-built python List method, count(), to check if the passed element exists in the List. If the passed element exists in the List, the count() method will show the number of times it occurs in the entire list. If it is a non-zero positive number, it means an element exists in the List.

How do you know if a value is not in list?

Use the not in operator to check if an element is not in a list. Use the syntax element not in list to return True if element is not in list and False otherwise.

How do you check if a string value exists in a list in Python?

Python Find String in List using count() We can also use count() function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list. l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C'] s = 'A' count = l1.


2 Answers

Use std::find:

std::find(l.begin(), l.end(), x) != l.end()
like image 124
MvG Avatar answered Sep 28 '22 18:09

MvG


Use std::find:

auto it = std::find(lst.begin(), lst.end(), x);
if ( it != lst.end() )
{
   //x found
}
like image 22
Nawaz Avatar answered Sep 28 '22 20:09

Nawaz