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 <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> v = { , , , , };
   auto isEven = [](int i){return i % != ;
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<int> v1 = { , , , , , };
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 <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main()
{
vector<int> v = { , , , , , };
auto isEven = [](int i){return i % == ;};
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 % != ;};
  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
the first odd is
the first odd is

  可以看到使用find_if_not不需要再定义新的否定含义的判断式了,更简便了。

  算法库还增加了一个copy_if算法,它相比原来的copy算法多了一个判断式,用起来更方便了,下面是它的基本用法:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main()
{
vector<int> v = { , , , , , };
std::vector<int> v1(v.size());
    //根据条件拷贝
    auto it = std::copy_if(v.begin(), v.end(), v1.begin(), [](int i){return i%!=;});
    //缩减vector到合适大小
  v1.resize(std::distance(v1.begin(),it));
for(int i : v1)
{
cout<<i<<" ";
} cout<<endl;
}

算法库新增了iota用来方便的生成有序序列,比如我们需要一个定长数组,这个数组中的元素都是在某一个数值的基础之上递增的,那么用iota可以很方便的生成这个数组了。下面是它的基本用法:

#include <numeric>
#include <array>
#include <vector>
#include <iostream>
using namespace std; int main()
{
vector<int> v() ;
//循环遍历赋值来初始化数组
//for(int i=1; i<=4; i++)
//{
// v.push_back(i);
//} //直接通过iota初始化数组,更简洁
std::iota(v.begin(), v.end(), );
for(auto n: v) {
cout << n << ' ';
}
cout << endl; std::array<int, > array;
std::iota(array.begin(), array.end(), );
for(auto n: array) {
cout << n << ' ';
}
std::cout << endl;
}

将输出:

   

  可以看到使用iota比遍历赋值来初始化数组更简洁,需要注意的是iota初始化的序列需要指定大小,如果上面的代码中:vector<int> v(4) ;没有指定初始化大小为4的话,则输出为空。

算法库还新增了一个同时获取最大值和最小值的算法minmax_element,这样我们如果想获取最大值和最小值的时候就不用分别调用max_element和max_element算法了,用起来会更方便,minmax_element会将最小值和最大值的迭代器放到一个pair中返回,下面是它的基本用法:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main() {
// your code goes here
vector<int> v = { , , , , , };
auto result = minmax_element(v.begin(), v.end()); cout<<*result.first<<" "<<*result.second<<endl; return ;
}

将输出:

1 9

算法库新增了is_ sorted和is_ sorted_until算法,is_sort用来判断某个序列是否是排好序的,is_sort_until则用来返回序列中前面已经排好序的部分序列。下面是它们的基本用法:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main() {
vector<int> v = { , , , , , };
auto pos = is_sorted_until(v.begin(), v.end()); for(auto it=v.begin(); it!=pos; ++it)
{
cout<<*it<< " ";
}
cout<<endl; bool is_sort = is_sorted(v.begin(), v.end());
cout<< is_sort<<endl;
return ;
}

将输出:


总结:这些新增的算法让我们用起来更加简便,也增强了代码的可读性。

c++11新增的一些便利的算法的更多相关文章

  1. hive 0.10 0.11新增特性综述

    我们的hive版本升迁经历了0.7.1 -> 0.8.1 -> 0.9.0,并且线上shark所依赖的hive版本也停留在0.9.0上,在这些版本上有我们自己的bug fix patch和 ...

  2. Java SE 11 新增特性

    Java SE 11 新增特性 作者:Grey 原文地址:Java SE 11 新增特性 源码 源仓库: Github:java_new_features 镜像仓库: GitCode:java_new ...

  3. C++11新增容器以及元组

    上次说了C++11的部分新特性,这里我们来说说新增的容器. unordered_map unordered_set unordered_multimap unordered_multiset arra ...

  4. C++11中list特有版本的算法

    与其他的容器不一样,链表类型的list和forward_list定义了几个成员函数形式的算法,这些函数和前面的所总结的通用算法不同,对于list来说,最好使用自己的特有算法,下面介绍一下主要的几个算法 ...

  5. nginx系列11:负载均衡哈希算法ip_hash与hash模块

    使用默认的round-robin负载均衡算法无法保证某一类请求只能由上游的某一台应用服务器处理,它只适用于AKF扩展中的水平扩展,如果要保证某一类请求只能由上游的某一台应用服务器处理,就需要用到AKF ...

  6. C++常量表达式、const、constexpr(C++11新增)的区别

    常量表达式是指值不会改变且在编译过程中就能够得到计算结果的表达式,能在编译时求值的表达式. 程序先编译再运行:  在编译阶段, 编译器将在编译过程中把用到该常量的地方都全都替换为 常量的值. 但是常量 ...

  7. 11.redis cluster的hash slot算法和一致性 hash 算法、普通hash算法的介绍

    分布式寻址算法 hash 算法(大量缓存重建) 一致性 hash 算法(自动缓存迁移)+ 虚拟节点(自动负载均衡) redis cluster 的 hash slot 算法 一.hash 算法 来了一 ...

  8. auto 和 decltype (C++11 新增)

    红色字体为个人推断,可信度自辨. 蓝色字体为重点. auto类型说明符:使用auto时,编译器会分析表达式,并自动推算出变量所属类型.*auto变量必须有初值 原理:编译器通过 初值 来判断auto变 ...

  9. C++11新特性应用--介绍几个新增的便利算法(用于分区的几个算法)

    今天继续. C++11新增的关于Non-modifying sequence operations和Modifying sequence operations的算法已经写了.具体信息见之前的博客. 以 ...

随机推荐

  1. Ubuntu18.04 运行 Gradle 4.9

    简介 Gradle是一个基于Apache Ant和Apache Maven的项目自动化构建工具, 使用一种基于Groovy的特定领域语言(domain-specific language DSL)来声 ...

  2. Windows开发进阶之VC++中如何实现对话框的界面重绘

    技术:Windows 系统+Visual studio 2008   概述 应用程序界面是用户与应用程序之间的交互的桥梁和媒介,用户界面是应用程序中最重要的组成部分,也是最为直观的视觉体现.对用户而言 ...

  3. 【mysql】关于InnoDB存储引擎 text blob 大字段的存储和优化

    最近在数据库优化的时候,看到一些表在设计上使用了text或者blob的字段,单表的存储空间已经达到了近100G,这种情况再去改变和优化就非常难了 一.简介 为了清楚大字段对性能的影响,我们必须要知道i ...

  4. java实现https ssl请求url

    import java.io.*;import java.net.*;import java.security.*;import java.security.cert.*;import java.ut ...

  5. samba config

    [global] netbios name = HARDY     #设置服务器的netbios名字 server string = my server #对samba服务器的描述 workgroup ...

  6. iOS linker command failed with exit code 1 (use -v to see invocation)多种解决方案汇总

    有时可能会遇到这种错误,关键是这种错误,有时只有这一句话,也不会给更多错误信息. 网上找了一些,总结了如下:(PS:以下是按照解决简易程度排序,不代表出现概率) 1.bitcode问题 解决如下:原因 ...

  7. C语言学习笔记 (010) - 编写strcpy函数

    很多公司的面试官在面试程序员的时候,要求应聘者写出库函数strcpy()的工作方式或者叫实现,很多人以为这个题目很简单,实则不然,别看这么一个小小的函数,它可以从三个方面来考查: (1)编程风格 (2 ...

  8. 【Spring】spring的7个模块

    Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架. Spring ...

  9. [译]async/await中使用阻塞式代码导致死锁 百万数据排序:优化的选择排序(堆排序)

    [译]async/await中使用阻塞式代码导致死锁 这篇博文主要是讲解在async/await中使用阻塞式代码导致死锁的问题,以及如何避免出现这种死锁.内容主要是从作者Stephen Cleary的 ...

  10. Error Code: 1030. Got error -1 from storage engine

    这个问题通常是数据库可以建表,旧表可以插入数据,正常:可是新表无法插入数据,无法改名等操作: 先从文件权限找方法,没法解决: 在网上搜了一通,大家都说的磁盘满了,但是我们的磁盘还空着呢! 后来,发现! ...