设为首页 加入收藏

TOP

c++11新增的一些便利的算法(一)
2015-07-20 17:44:28 来源: 作者: 【 】 浏览:4
Tags:新增 一些 便利 算法
c++11新增加了一些便利的算法,这些新增的算法使我们的代码写起来更简洁方便,这里仅仅列举一些常用的新增算法,算是做个总结,?
?
  算法库新增了三个用于判断的算法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 << "all is odd" << endl;
?
? ? ? ?bool isNoneEven = std::none_of(v.begin(), v.end(), isEven);
? ? ? ?if (isNoneEven)
? ? ? ? ? ? ? cout << "none is even" << endl;
?
? ? ? ?vector v1 = { 1, 3, 5, 7, 8, 9 };
? ? ? ?bool anyof = std::any_of(v1.begin(), v1.end(), isEven);
? ? ? ?if (anyof)
? ? ? ? ? ? ? cout << "at least one is even" << endl;
}
复制代码
输出:
?
all is odd
none is odd
at least one is even
? ? ? ?算法库的查找算法新增了一个find_if_not,它的含义和find_if是相反的,即查找不符合某个条件的元素,find_if也可以实现find_if_not的功能,只需要将判断式改为否定的判断式即可,现在新增了find_if_not之后,就不需要再写否定的判断式了,可读性也变得更好。下面是它的基本用法:
?
复制代码
#include
#include
#include
using namespace std;
?
int main()
{
? ? vector v = { 1, 3, 5, 7, 9,4 };
? ? auto isEven = [](int i){return i % 2 == 0;};
? ? auto firstEven = std::find_if(v.begin(), v.end(), isEven);
? ? if (firstEven!=v.end())
? ? ? ? ? ? ? cout << "the first even is " <<* firstEven << endl;
?
? ? ? ?//用find_if来查找奇数则需要重新写一个否定含义的判断式
  auto isNotEven = [](int i){return i % 2 != 0;};
  auto firstOdd = std::find_if(v.begin(), v.end(),isNotEven);
?
? ? ? ?if (firstOdd!=v.end())
? ? ? ? ? ? ? cout << "the first odd is " <<* firstOdd << endl;
?
? ? ? ?//用find_if_not来查找奇数则无需新定义判断式
?
? ? ? ?auto odd = std::find_if_not(v.begin(), v.end(), isEven);
? ? ? ?if (odd!=v.end())
? ? ? ? ? ? ? cout << "the first odd is " <<* odd << endl;
}
复制代码
将输出:
?
the first even is 4
the first odd is 1
the first odd is 1
  可以看到使用find_if_not不需要再定义新的否定含义的判断式了,更简便了。
?
  算法库还增加了一个copy_if算法,它相比原来的copy算法多了一个判断式,用起来更方便了,下面是它的基本用法:
?
复制代码
#include
#include
#include
using namespace std;
?
int main()
{
? ? ? ?vector v = { 1, 3, 5, 7, 9, 4 };
? ? ? ?std::vector v1(v.size());
    //根据条件拷贝
    auto it = std::copy_if(v.begin(), v.end(), v1.begin(), [](int i){return i%2!=0;});
    //缩减vector到合适大小
? ?   v1.resize(std::distance(v1.begin(),it));
? ? ? ?for(int i : v1)
? ? ? ?{
? ? ? ? ? ? ? cout<
? ? ? ?}
?
? ? ? ?cout<
} ? ?
复制代码
? ? ? ?算法库新增了iota用来方便的生成有序序列,比如我们需要一个定长数组,这个数组中的元素都是在某一个数值的基础之上递增的,那么用iota可以很方便的生成这个数组了。下面是它的基本用法:
?
复制代码
#include
#include
#include
#include
using namespace std;
?
int main()
{
vector v(4) ;
//循环遍历赋值来初始化数组
//for(int i=1; i<=4; i++)
//{
// ? ?v.push_back(i);
//}
?
//直接通过iota初始化数组,更简洁
? ? std::iota(v.begin(), v.end(), 1);
? ? for(auto n: v) {
? ? ? ? cout << n << ' ';
? ? }
? ? cout << endl;
? ??
? ? std::array array;
? ? std::iota(array.begin(), array.end(), 1);
? ? for(auto n: array) {
? ? ? ? cout << n << ' ';
? ? }
? ? std::cout << endl;
}
复制代码
将输出:
?
1 2 3 4
1 2 3 4
  可以看到使用iota比遍历赋值来初始化数组更简洁,需要注意的是iota初始化的序列需要指定大小,如果上面的代码中:vector v(4) ;没有指定初始化大小为4的话,则输出为空。
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj 2570 Fiber Network (Floyd) 下一篇Accelerated C++ 学习笔记及题解-..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·常用meta整理 | 菜鸟 (2025-12-25 01:21:52)
·SQL HAVING 子句:深 (2025-12-25 01:21:47)
·SQL CREATE INDEX 语 (2025-12-25 01:21:45)
·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)