如何检查一个元素是否在集合中?
是否有与以下代码更简单的等效代码:
myset.find(x) != myset.end()
如何检查一个元素是否在集合中?
是否有与以下代码更简单的等效代码:
myset.find(x) != myset.end()
当前回答
Just to clarify, the reason why there is no member like contains() in these container types is because it would open you up to writing inefficient code. Such a method would probably just do a this->find(key) != this->end() internally, but consider what you do when the key is indeed present; in most cases you'll then want to get the element and do something with it. This means you'd have to do a second find(), which is inefficient. It's better to use find directly, so you can cache your result, like so:
auto it = myContainer.find(key);
if (it != myContainer.end())
{
// Do something with it, no more lookup needed.
}
else
{
// Key was not present.
}
当然,如果你不关心效率,你总是可以自己滚动,但在这种情况下,你可能不应该使用c++…;)
其他回答
就是这个,一英里。
bool once(uintptr_t val) {
return visited.emplace(val).second;
}
怎么会不是这样呢?
https://godbolt.org/z/9zP77jqMc
func5(unsigned long):
sub rsp, 24
mov QWORD PTR [rsp+8], rdi
lea rsi, [rsp+8]
mov edi, OFFSET FLAT:visited2
call std::pair<std::_Rb_tree_iterator<unsigned long>, bool> std::_Rb_tree<unsigned long, unsigned long, std::_Identity<unsigned long>, std::less<unsigned long>, std::allocator<unsigned long> >::_M_emplace_unique<unsigned long&>(unsigned long&)
add rsp, 24
mov eax, edx
ret
Just to clarify, the reason why there is no member like contains() in these container types is because it would open you up to writing inefficient code. Such a method would probably just do a this->find(key) != this->end() internally, but consider what you do when the key is indeed present; in most cases you'll then want to get the element and do something with it. This means you'd have to do a second find(), which is inefficient. It's better to use find directly, so you can cache your result, like so:
auto it = myContainer.find(key);
if (it != myContainer.end())
{
// Do something with it, no more lookup needed.
}
else
{
// Key was not present.
}
当然,如果你不关心效率,你总是可以自己滚动,但在这种情况下,你可能不应该使用c++…;)
如果你要添加一个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();
}
这适用于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));
}
编辑:
正如评论中指出的,我无意中使用了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);
}
}
检查许多STL容器是否存在的典型方法,如std::map, std::set,…是:
const bool is_in = container.find(element) != container.end();
另一种简单地判断元素是否存在的方法是检查count()
if (myset.count(x)) {
// x is in the set, count is 1
} else {
// count zero, i.e. x not in the set
}
然而,大多数时候,我发现自己需要访问元素,无论我在哪里检查它的存在。
所以我还是要找到迭代器。当然,最好也把它和end进行比较。
set< X >::iterator it = myset.find(x);
if (it != myset.end()) {
// do something with *it
}
C + + 20
在c++ 20中,set获得一个contains函数,因此如下所述成为可能:https://stackoverflow.com/a/54197839/895245
if (myset.contains(x)) {
// x is in the set
} else {
// no x
}