ducin
ducin

Reputation: 26437

Check if element is in the list (contains)

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 = [1,2,3,4] # elements
my_var = 3 # my variable
my_var in my_list # returns boolean

How to do that in C++? I thought of using std::list, but I can find no find method in it. I can see such method in std::set structure.

More deeply, the problem is that my program is given some unique ids (a list, a set, whatever) and I iterate over a long list of input data (ids) and check if they are included in the list (boolean value returned for each iteration step). And I'm not sure how should I do that in C++.

Upvotes: 71

Views: 228451

Answers (9)

Michaël
Michaël

Reputation: 530

In C++23, you can simply use std::ranges::contains. Adapting the example therein to lists:

#include <algorithm>
#include <list>
#include <cassert>

int main()
{
    auto haystack = std::list{3, 1, 4, 1, 5};
    auto needle = std::list{1, 4, 1};
    auto bodkin = std::list{2, 5, 2};
    auto increment = [](int x) { return ++x; };
    auto decrement = [](int x) { return --x; };
 
  assert (
        std::ranges::contains(haystack, 4) &&
       !std::ranges::contains(haystack, 6) &&
        std::ranges::contains_subrange(haystack, needle) &&
       !std::ranges::contains_subrange(haystack, bodkin) &&
        std::ranges::contains(haystack, 6, increment) &&
       !std::ranges::contains(haystack, 1, increment) &&
        std::ranges::contains_subrange(haystack, bodkin, {}, increment) &&
       !std::ranges::contains_subrange(haystack, bodkin, {}, decrement) &&
        std::ranges::contains_subrange(haystack, bodkin, {}, {}, decrement));
}

Upvotes: 4

ZyReS
ZyReS

Reputation: 9

Since C++20 you can use l.contains(my_var)

https://www.modernescpp.com/index.php/more-convenience-functions-for-containers-with-c-20

Upvotes: 0

Markus Dutschke
Markus Dutschke

Reputation: 10606

A one-liner solution, similar to python, would be (std::set<int> {1, 2, 3, 4}).count(my_var) > 0.

Minimal working example

int my_var = 3;
bool myVarIn = (std::set<int> {1, 2, 3, 4}).count(my_var) > 0;
std::cout << std::boolalpha << myVarIn << std::endl;

prints true or false dependent of the value of my_var.

Upvotes: 6

Rakib
Rakib

Reputation: 7625

std::list does not provide a search method. You can iterate over the list and check if the element exists or use std::find. But I think for your situation std::set is more preferable. The former will take O(n) time but later will take O(lg(n)) time to search.

You can simply use:

int my_var = 3;
std::set<int> mySet {1, 2, 3, 4};
if(mySet.find(myVar) != mySet.end()){
      //do whatever
}

Upvotes: 35

TarmoPikaro
TarmoPikaro

Reputation: 5233

Declare additional helper function like this:

template <class T, class I >
bool vectorContains(const vector<T>& v, I& t)
{
    bool found = (std::find(v.begin(), v.end(), t) != v.end());
    return found;
}

And use it like this:

void Project::AddPlatform(const char* platform)
{
    if (!vectorContains(platforms, platform))
        platforms.push_back(platform);
}

Snapshot of example can be found here:

https://github.com/tapika/cppscriptcore/blob/b7f3d62747494a52a440482e841ffb016a3fc56e/SolutionProjectModel/Project.cpp#L13

Upvotes: 2

KungPhoo
KungPhoo

Reputation: 754

They really should add a wrapper. Like this:

namespace std
{
    template<class _container,
        class _Ty> inline
        bool contains(_container _C, const _Ty& _Val)
        {return std::find(_C.begin(), _C.end(), _Val) != _C.end(); }
};
...
    if( std::contains(my_container, what_to_find) )
    {

    }

Upvotes: 11

Matzi
Matzi

Reputation: 13925

You can use std::find

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

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

Upvotes: 120

Paul Evans
Paul Evans

Reputation: 27567

Use std::find, something like:

if (std::find(std::begin(my_list), std::end(my_list), my_var) != std::end(my_list))
    // my_list has my_var

Upvotes: 2

Radu Chivu
Radu Chivu

Reputation: 1065

you must #include <algorithm>, then you can use std::find

Upvotes: 9

Related Questions