日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

c语言中的set是置1嘛,c ++ - 如何检查元素是否在std :: set中?

發布時間:2025/3/12 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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中?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。