Check if element is in the list (contains)

后端 未结 7 1951
南笙
南笙 2021-01-31 14:38

I\'ve got a list of elements, say, integers and I want to check if my variable (another integer) is one of the elements from the list. In python I\'d do:

my_list         


        
7条回答
  •  温柔的废话
    2021-01-31 15:25

    You can use std::find

    bool found = (std::find(my_list.begin(), my_list.end(), my_var) != my_list.end());
    

    You need to include . It should work on standard containers, vectors lists, etc...

提交回复
热议问题