How to check that an element is in a std::set?

前端 未结 10 1953
春和景丽
春和景丽 2020-11-30 17:13

How do you check that an element is in a set?

Is there a simpler equivalent of the following code:

myset.find(x) != myset.end()
相关标签:
10条回答
  • 2020-11-30 17:25

    You can also check whether an element is in set or not while inserting the element. The single element version return a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the equivalent element already in the set. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent element already existed.

    For example: Suppose the set already has 20 as an element.

     std::set<int> myset;
     std::set<int>::iterator it;
     std::pair<std::set<int>::iterator,bool> ret;
    
     ret=myset.insert(20);
     if(ret.second==false)
     {
         //do nothing
    
     }
     else
     {
        //do something
     }
    
     it=ret.first //points to element 20 already in set.
    

    If the element is newly inserted than pair::first will point to the position of new element in set.

    0 讨论(0)
  • 2020-11-30 17:26

    I use

    if(!my_set.count(that_element)) //Element is present...
    ;
    

    But it is not as efficient as

    if(my_set.find(that_element)!=my_set.end()) ....;
    

    My version only saves my time in writing the code. I prefer it this way for competitive coding.

    0 讨论(0)
  • 2020-11-30 17:27

    Another way of simply telling if an element exists is to check the count()

    if (myset.count(x)) {
       // x is in the set, count is 1
    } else {
       // count zero, i.e. x not in the set
    }
    

    Most of the times, however, I find myself needing access to the element wherever I check for its existence.

    So I'd have to find the iterator anyway. Then, of course, it's better to simply compare it to end too.

    set< X >::iterator it = myset.find(x);
    if (it != myset.end()) {
       // do something with *it
    }
    

    C++ 20

    In C++20 set gets a contains function, so the following becomes possible as mentioned at: https://stackoverflow.com/a/54197839/895245

    if (myset.contains(x)) {
      // x is in the set
    } else {
      // no x 
    }
    
    0 讨论(0)
  • 2020-11-30 17:28

    Write your own:

    template<class T>
    bool checkElementIsInSet(const T& elem, const std::set<T>& container)
    {
      return container.find(elem) != container.end();
    }
    
    0 讨论(0)
  • 2020-11-30 17:28

    I was able to write a general contains function for std::list and std::vector,

    template<typename T>
    bool contains( const list<T>& container, const T& elt )
    {
      return find( container.begin(), container.end(), elt ) != container.end() ;
    }
    
    template<typename T>
    bool contains( const vector<T>& container, const T& elt )
    {
      return find( container.begin(), container.end(), elt ) != container.end() ;
    }
    
    // use:
    if( contains( yourList, itemInList ) ) // then do something
    

    This cleans up the syntax a bit.

    But I could not use template template parameter magic to make this work arbitrary stl containers.

    // NOT WORKING:
    template<template<class> class STLContainer, class T>
    bool contains( STLContainer<T> container, T elt )
    {
      return find( container.begin(), container.end(), elt ) != container.end() ;
    }
    

    Any comments about improving the last answer would be nice.

    0 讨论(0)
  • 2020-11-30 17:30

    If you were going to add a contains function, it might look like this:

    #include <algorithm>
    #include <iterator>
    
    template<class TInputIterator, class T> inline
    bool contains(TInputIterator first, TInputIterator last, const T& value)
    {
        return std::find(first, last, value) != last;
    }
    
    template<class TContainer, class T> inline
    bool contains(const TContainer& container, const T& value)
    {
        // This works with more containers but requires std::begin and std::end
        // from C++0x, which you can get either:
        //  1. By using a C++0x compiler or
        //  2. Including the utility functions below.
        return contains(std::begin(container), std::end(container), value);
    
        // This works pre-C++0x (and without the utility functions below, but doesn't
        // work for fixed-length arrays.
        //return contains(container.begin(), container.end(), value);
    }
    
    template<class T> inline
    bool contains(const std::set<T>& container, const T& value)
    {
        return container.find(value) != container.end();
    }
    

    This works with std::set, other STL containers, and even fixed-length arrays:

    void test()
    {
        std::set<int> set;
        set.insert(1);
        set.insert(4);
        assert(!contains(set, 3));
    
        int set2[] = { 1, 2, 3 };
        assert(contains(set2, 3));
    }
    

    Edit:

    As pointed out in the comments, I unintentionally used a function new to C++0x (std::begin and std::end). Here is the near-trivial implementation from VS2010:

    namespace std {
    
    template<class _Container> inline
        typename _Container::iterator begin(_Container& _Cont)
        { // get beginning of sequence
        return (_Cont.begin());
        }
    
    template<class _Container> inline
        typename _Container::const_iterator begin(const _Container& _Cont)
        { // get beginning of sequence
        return (_Cont.begin());
        }
    
    template<class _Container> inline
        typename _Container::iterator end(_Container& _Cont)
        { // get end of sequence
        return (_Cont.end());
        }
    
    template<class _Container> inline
        typename _Container::const_iterator end(const _Container& _Cont)
        { // get end of sequence
        return (_Cont.end());
        }
    
    template<class _Ty,
        size_t _Size> inline
        _Ty *begin(_Ty (&_Array)[_Size])
        { // get beginning of array
        return (&_Array[0]);
        }
    
    template<class _Ty,
        size_t _Size> inline
        _Ty *end(_Ty (&_Array)[_Size])
        { // get end of array
        return (&_Array[0] + _Size);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题