c++11新增的一些便利的算法

您所在的位置:网站首页 衡水中学全景图高清 c++11新增的一些便利的算法

c++11新增的一些便利的算法

2024-01-12 22:03| 来源: 网络整理| 查看: 265

  c++11新增加了一些便利的算法,这些新增的算法使我们的代码写起来更简洁方便,这里仅仅列举一些常用的新增算法,算是做个总结,更多的新增算法读者可以参考http://en.cppreference.com/w/cpp/algorithm。

  算法库新增了三个用于判断的算法all_of、any_of和none_of:

template< class InputIt, class UnaryPredicate > bool all_of( InputIt first, InputIt last, UnaryPredicate p ); template< class InputIt, class UnaryPredicate > bool any_of( InputIt first, InputIt last, UnaryPredicate p ); template< class InputIt, class UnaryPredicate > bool none_of( InputIt first, InputIt last, UnaryPredicate p ); all_of:检查区间[first, last)中是否所有的元素都满足一元判断式p,所有的元素都满足条件返回true,否则返回false。 any_of:检查区间[first, last)中是否至少有一个元素都满足一元判断式p,只要有一个元素满足条件就返回true,否则返回true。 none_of:检查区间[first, last)中是否所有的元素都不满足一元判断式p,所有的元素都不满足条件返回true,否则返回false。

下面是这几个算法的示例:

#include #include #include using namespace std; int main() { vector v = { 1, 3, 5, 7, 9 };    auto isEven = [](int i){return i % 2 != 0; bool isallOdd = std::all_of(v.begin(), v.end(), isEven); if (isallOdd) cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3