ful*_*ton 295 c++ stl contains set
你如何检查元素是否在集合中?
是否有更简单的等效代码如下:
myset.find(x) != myset.end()
Run Code Online (Sandbox Code Playgroud)
unw*_*ind 361
在许多STL容器中检查存在的典型方法是:
const bool is_in = container.find(element) != container.end();
Run Code Online (Sandbox Code Playgroud)
Pie*_*ter 191
简单地告诉元素是否存在的另一种方法是检查 count()
if (myset.count(x)) {
// x is in the set, count is 1
} else {
// count zero, i.e. x not in the set
}
Run Code Online (Sandbox Code Playgroud)
但是,大多数时候,无论我在哪里检查它的存在,我都会发现自己需要访问该元素.
所以我无论如何都要找到迭代器.然后,当然,最好简单地比较一下end.
set< X >::iterator it = myset.find(x);
if (it != myset.end()) {
// do something with *it
}
Run Code Online (Sandbox Code Playgroud)
Tim*_*Tim 37
只是为了澄清,contains()在这些容器类型中没有成员的原因是因为它会打开你写低效的代码.这样的方法可能只是在this->find(key) != this->end()内部进行,但考虑当密钥确实存在时你做了什么; 在大多数情况下,您将需要获取元素并使用它执行某些操作.这意味着你必须做一秒钟find(),这是低效的.最好直接使用find,这样就可以缓存结果,如下所示:
Container::const_iterator it = myContainer.find(key);
if (it != myContainer.end())
{
// Do something with it, no more lookup needed.
}
else
{
// Key was not present.
}
Run Code Online (Sandbox Code Playgroud)
当然,如果你不关心效率,你总是可以自己动手,但在这种情况下你可能不应该使用C++ ...;)
在C ++ 20中,我们最终将获得std::set::contains方法。
#include <iostream>
#include <string>
#include <set>
int main()
{
std::set<std::string> example = {"Do", "not", "panic", "!!!"};
if(example.contains("panic")) {
std::cout << "Found\n";
} else {
std::cout << "Not found\n";
}
}
Run Code Online (Sandbox Code Playgroud)
如果您要添加一个contains函数,它可能如下所示:
#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();
}
Run Code Online (Sandbox Code Playgroud)
这适用于std::set其他STL容器,甚至是固定长度的数组:
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));
}
Run Code Online (Sandbox Code Playgroud)
正如评论中指出的那样,我无意中使用了一个新的C++ 0x(std::begin和std::end)函数.以下是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);
}
}
Run Code Online (Sandbox Code Playgroud)
从 C++20 开始,就只有(最后!) bool std::contains(const K&)
https://en.cppreference.com/w/cpp/container/set/contains