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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值