c语言中的set是置1嘛,c ++ - 如何检查元素是否在std :: set中?
如果您要添加std::set函數,它可能如下所示:
#include
#include
template inline
bool contains(TInputIterator first, TInputIterator last, const T& value)
{
return std::find(first, last, value) != last;
}
template 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 inline
bool contains(const std::set& container, const T& value)
{
return container.find(value) != container.end();
}
這適用于std::set,其他STL容器,甚至固定長度數組:
void test()
{
std::set 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 inline
typename _Container::iterator begin(_Container& _Cont)
{ // get beginning of sequence
return (_Cont.begin());
}
template inline
typename _Container::const_iterator begin(const _Container& _Cont)
{ // get beginning of sequence
return (_Cont.begin());
}
template inline
typename _Container::iterator end(_Container& _Cont)
{ // get end of sequence
return (_Cont.end());
}
template inline
typename _Container::const_iterator end(const _Container& _Cont)
{ // get end of sequence
return (_Cont.end());
}
template
size_t _Size> inline
_Ty *begin(_Ty (&_Array)[_Size])
{ // get beginning of array
return (&_Array[0]);
}
template
size_t _Size> inline
_Ty *end(_Ty (&_Array)[_Size])
{ // get end of array
return (&_Array[0] + _Size);
}
}
總結
以上是生活随笔為你收集整理的c语言中的set是置1嘛,c ++ - 如何检查元素是否在std :: set中?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: flex 固定一列_css实现两列固定与
- 下一篇: mysql 查询语句 过滤_MySQL全