总所周知。C++ STL中有个头文件,名为algorithm。即算法的意思。

The header<algorithm>defines a collection of functions especially designed to be used on ranges of elements.

所以,要八一八这个头文件里C++11新增的几个算法,今天主要描写叙述的几个算法不改变容器中元素的顺序。

这里还要啰嗦一句,使用stl算法时,假设与lambda表达式组合使用,那么代码会更加简洁。

find_if_not

该算法在之前介绍过,请參阅博客《实战c++中的vector系列–vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)》。

all_of

原型:

template <class InputIterator, class UnaryPredicate>
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);

作用:

Test condition on all elements in range

Returns true if pred returns true for all the elements in the range [first,last) or if the range is empty, and false otherwise.

检測区间[first, last)中是否全部的元素都满足一元推断表达式pred。全部的元素都满足条件返回true,否则返回false.

应用:

#include <iostream>     // std::cout
#include <algorithm> // std::all_of
#include <array> // std::array int main () {
std::array<int,8> foo = {3,5,7,11,13,17,19,23}; if ( std::all_of(foo.begin(), foo.end(), [](int i){return i%2;}) )
std::cout << "All the elements are odd numbers.\n"; return 0;
}
//输出:
All the elements are odd numbers.

any_of

原型:

template <class InputIterator, class UnaryPredicate>
bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);

作用:

Test if any element in range fulfills condition

Returns true if pred returns true for any of the elements in the range [first,last), and false otherwise.

应用:

#include <iostream>     // std::cout
#include <algorithm> // std::any_of
#include <array> // std::array int main () {
std::array<int,7> foo = {0,1,-1,3,-3,5,-5}; if ( std::any_of(foo.begin(), foo.end(), [](int i){return i<0;}) )
std::cout << "There are negative elements in the range.\n"; return 0;
}
//输出:
There are negative elements in the range.

none_of

原型:

template <class InputIterator, class UnaryPredicate>
bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred);

作用:

Returns true if pred returns false for all the elements in the range [first,last) or if the range is empty, and false otherwise.

应用:

#include <iostream>     // std::cout
#include <algorithm> // std::none_of
#include <array> // std::array int main () {
std::array<int,8> foo = {1,2,4,8,16,32,64,128}; if ( std::none_of(foo.begin(), foo.end(), [](int i){return i<0;}) )
std::cout << "There are no negative elements in the range.\n"; return 0;
}
//输出:
There are no negative elements in the range.

is_permutation

permutation 名词 排列 、交换等意思。

该函数是用来推断两个序列是否为同一元素集的不同排列。

该函数使用operator==或者是pred来推断两个元素是否是相等的。

原型:

template <class ForwardIterator1, class ForwardIterator2>
bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2);
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, BinaryPredicate pred);

作用:

Test whether range is permutation of another

Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if all of the elements in both ranges match, even in a different order.

应用:

#include <iostream>     // std::cout
#include <algorithm> // std::is_permutation
#include <array> // std::array int main () {
std::array<int,5> foo = {1,2,3,4,5};
std::array<int,5> bar = {3,1,4,5,2}; if ( std::is_permutation (foo.begin(), foo.end(), bar.begin()) )
std::cout << "foo and bar contain the same elements.\n"; return 0;
}
//输出:
foo and bar contain the same elements.

这里须要注意的是。你不能突发奇想,比方用大于号或是小于号进行比較,你仅仅能这样:

The elements are compared using operator== (or pred)

C++11新特性应用--介绍几个新增的便利算法(不更改容器中元素顺序的算法)的更多相关文章

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

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

  2. C++11新特性应用--介绍几个新增的便利算法(用于排序的几个算法)

    继续C++11在头文件algorithm中添加的算法. 至少我认为,在stl的算法中,用到最多的就是sort了,我们不去探索sort的源代码.就是介绍C++11新增的几个关于排序的函数. 对于一个序列 ...

  3. Java 11 新特性介绍

    Java 11 已于 2018 年 9 月 25 日正式发布,之前在Java 10 新特性介绍中介绍过,为了加快的版本迭代.跟进社区反馈,Java 的版本发布周期调整为每六个月一次——即每半年发布一个 ...

  4. C++11新特性总结 (二)

    1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...

  5. [转载] C++11新特性

    C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...

  6. C++11 新特性之智能指针(shared_ptr, unique_ptr, weak_ptr)

    这是C++11新特性介绍的第五部分,涉及到智能指针的相关内容(shared_ptr, unique_ptr, weak_ptr). shared_ptr shared_ptr 基本用法 shared_ ...

  7. 【C++11新特性】 C++11智能指针之weak_ptr

    如题,我们今天要讲的是C++11引入的三种智能指针中的最后一个:weak_ptr.在学习weak_ptr之前最好对shared_ptr有所了解.如果你还不知道shared_ptr是何物,可以看看我的另 ...

  8. Redis 6.0 新特性 ACL 介绍

    Redis 6.0 新特性 ACL 介绍 Intro 在 Redis 6.0 中引入了 ACL(Access Control List) 的支持,在此前的版本中 Redis 中是没有用户的概念的,其实 ...

  9. c++ 11 线程池---完全使用c++ 11新特性

    前言: 目前网上的c++线程池资源多是使用老版本或者使用系统接口实现,使用c++ 11新特性的不多,最近研究了一下,实现一个简单版本,可实现任意任意参数函数的调用以及获得返回值. 0 前置知识 首先介 ...

随机推荐

  1. !DOCTYPE 文档类型声明

      1.用途 Web 世界中存在许多不同的文档.只有了解文档的类型,浏览器才能正确地显示文档.HTML 也有多个不同的版本,只有完全明白页面中使用的确切 HTML 版本,浏览器才能完全正确地显示出 H ...

  2. 【IDEA】向IntelliJ IDEA创建的项目导入Jar包的两种方式

    转载请注明出处:http://blog.csdn.net/qq_26525215 本文源自[大学之旅_谙忆的博客] 今天用IDEA,需要导入一个Jar包,因为以前都是用eclipse的,所以对这个id ...

  3. TF30042: The database is full. Contact your Team Foundation Server administrator.

    TF30042: The database is full. Contact your Team Foundation Server administrator. 在一个阳光明媚的下午,迁入代码的时候 ...

  4. 最小化安装linux CentOS_7操作系统

    实验环境为VMware虚拟机安装操作系统. 1.打开VMware Workstation 虚拟机,选择创建新的虚拟机: 2.选择linux-CentOS 64位操作系统: 3.为虚拟机命名,并选择安装 ...

  5. WPF字符串中的换行符

    <sys:String x:Key="NewUpdateWillShow" xml:space="preserve">第一行 第二行 </sy ...

  6. StackExchange.Redis学习笔记(三) 数据库及密码配置 GetServer函数

    这一章主要写一些StackExchange.Redis的配置及不太经常用到的函数 数据库连接 下面是我的连接字符串,里面指定了地址,密码,及默认的数据库 Redis启动后默认会分成0-15个数据库,不 ...

  7. .net SignalR winform 推送广播

    最近在做一个项目,需要用到服务端主动推送给客户端,最开始用的是自己比较顺手的Remoting,可后来发现把服务端架到外网上,就猴子它哥了,后来又尝试WCF,虽然能推送,但是推了几次也猴子它哥了,后来找 ...

  8. Redis安装及使用笔记

    windows下安装Redis 1.下载Redis的软件包 Redis on github; 2.将软件解压到服务器软件目录; 3.在命令行运行此命令: ./redis-server redis.wi ...

  9. Python后端开发要求

    关于Python后端开发要求 一.对Python有兴趣,熟悉Python(标准库) 最好阅读过源码 了解Python的优化(熟悉pypy更佳) 二.至少至少一门语言(不说"精通") ...

  10. android shape 大全 (转)

    1. 各属性的配置语法 在项目 res/drawable 文件夹中创建一个以 shape 为根节点的 XML 文件,基本语法如下: <?xml version="1.0" e ...