Jak sprawdzić, czy element znajduje się w std::set?

Jak sprawdzić, czy element jest w zbiorze?

Czy istnieje prostszy odpowiednik następującego kodu:

myset.find(x) != myset.end()
Author: Lightness Races in Orbit, 2009-11-09

9 answers

Typowym sposobem sprawdzenia istnienia w wielu kontenerach STL jest:

const bool is_in = container.find(element) != container.end();
 300
Author: unwind,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-09 17:39:19

Innym sposobem po prostu powiedzieć, czy element istnieje jest sprawdzenie count()

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

W większości przypadków jednak potrzebuję dostępu do pierwiastka wszędzie tam, gdzie sprawdzam jego istnienie.

Więc i tak musiałbym znaleźć iterator. Następnie, oczywiście, lepiej jest po prostu porównać go do end zbyt.
set< X >::iterator it = myset.find(x);
if (it != myset.end()) {
   // do something with *it
}
 153
Author: Pieter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-09 15:42:31

Dla jasności, powodem, dla którego nie ma takiego członka jak contains() w tych typach kontenerów jest to, że otworzyłoby cię to na pisanie nieefektywnego kodu. Taka metoda prawdopodobnie wykonałaby this->find(key) != this->end() wewnętrznie, ale zastanów się, co robisz, gdy klucz jest rzeczywiście obecny; w większości przypadków będziesz chciał uzyskać element i coś z nim zrobić. Oznacza to, że musiałbyś zrobić drugą find(), która jest nieefektywna. Lepiej użyć find bezpośrednio, więc możesz buforować wynik, jak więc:

Container::const_iterator it = myContainer.find(key);
if (it != myContainer.end())
{
  // Do something with it, no more lookup needed.
}
else
{
  // Key was not present.
}

Oczywiście, jeśli nie zależy ci na wydajności, zawsze możesz rzucić własną, ale w takim przypadku prawdopodobnie nie powinieneś używać C++... ;)

 35
Author: Tim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-09 16:26:59

Jeśli zamierzasz dodać contains funkcję, może ona wyglądać tak:

#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();
}

Działa z std::set, innymi kontenerami STL, a nawet tablicami o stałej długości:

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:

Jak zaznaczono w komentarzach, niechcący użyłem funkcji nowej W C++0x (std::begin i std::end). Oto prawie trywialna implementacja z 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);
    }

}
 6
Author: Sam Harwell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-09 17:21:45

Możesz również sprawdzić, czy element jest w zestawie, czy nie podczas wstawiania elementu. Wersja z pojedynczym elementem zwraca parę, z jej parą prętową::First ustawioną na iterator wskazujący albo na nowo wstawiony element, albo na równoważny element już w zestawie. Pair:: drugi element w parze jest ustawiony na true, jeśli nowy element został wstawiony lub false, jeśli równoważny element już istniał.

Na przykład: załóżmy, że zbiór ma już 20 jako 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.

Jeśli element jest nowo wstawiony niż pair:: first wskaże pozycję nowego elementu w zestawie.

 3
Author: Prashant Shubham,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-15 17:42:01

Napisz własny:

template<class T>
bool checkElementIsInSet(const T& elem, const std::set<T>& container)
{
  return container.find(elem) != container.end();
}
 2
Author: stefaanv,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-09 13:59:28

Używam

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

Ale nie jest tak skuteczny jak

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

Moja wersja oszczędza tylko mój czas na pisanie kodu. Wolę to w ten sposób do konkurencyjnego kodowania.

 1
Author: Manas Bondale,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-03 10:41:00

Udało mi się napisać ogólną contains funkcję dla std::list i 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

To trochę oczyszcza składnię.

Ale nie mogłem użyć template parametr template magic aby to działało dowolne kontenery stl.

// 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() ;
}

Wszelkie uwagi dotyczące poprawy ostatniej odpowiedzi byłyby mile widziane.

 0
Author: bobobobo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 10:31:38

/ / składnia ogólna

       set<int>::iterator ii = find(set1.begin(),set1.end(),"element to be searched");

/* w poniższym kodzie próbuję znaleźć element 4 in i INT set czy jest obecny czy nie* /

set<int>::iterator ii = find(set1.begin(),set1.end(),4);
 if(ii!=set1.end())
 {
    cout<<"element found";
    set1.erase(ii);// in case you want to erase that element from set.
 }
 0
Author: sanjeev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-04 11:27:13