总所周知。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. android视频播放器系列(一)——系统播放器

    使用系统播放器(intent隐士调用)可以播放本地视频和网络视频,但是使用方式上稍微有点差别: 一.播放本地视频 Uri uri = Uri.parse("本地视频地址");Int ...

  2. JavaSE高级1

    内部类 内部类概念: 所谓内部类(Inner Class),顾名思义,就是将一个类定义在另一个类的内部.内部的类称之为内部类. 内部类的主要特点: 内部类可以很好的实现隐藏,可以使用protected ...

  3. 呵呵哒,LNMP下通过fread方式下载文件时,中文名称文件找不到文件

    哎,整整折腾一个下午. 本来好好的,thinkphp 自动的uniq方式保存的文件名,非要使用原文件名,真心蛋疼~~ 然后就只好写个脚本 把原来的所有文件都重新命名一下 - - 然后把数据库对应字段也 ...

  4. JavaScript正则表达式知识点

    通过学习imooc课程<JavaScript正则表达式>http://www.imooc.com/video/12539,对视频教学内容做一个知识整理. 一个正则表达式在线工具:http: ...

  5. PHP基础入门(三)【PHP中的数组】

    PHP数组的分类 按照下标的不同,PHP数组分为关联数组与索引数组: 索引数组:下标从0开始,依次增长: 关联数组: 下标为字符串格式,每个下标字符串与数组的值一一关联对应.(有点像对象的键值对) 关 ...

  6. MUI框架开发HTML5手机APP(一)--搭建第一个手机APP

      前  言 JRedu 随着HTML5的不断发展,移动开发成为主流趋势!越来越多的公司开始选择使用HTML5开发手机APP,而随着手机硬件设备配置的不断提升,各种开发框架的不断优化,也使着H5开发的 ...

  7. Docker安装和卸载

    一:卸载旧版本 老版本的Docker被称为docker或docker-engine.如果安装了这些,请卸载它们以及相关的依赖项. $ sudo yum remove docker \ docker-c ...

  8. C#中抽象类和接口的区别3

    一.普通类和抽象类之间的异同 1.都可以被继承 2.抽象类不能被实例化,只是用来继承的.普通类可以实例化 3.抽象方法只有含方法声明而没有方法体且必须包含在抽象类里面 4.子类继承抽象类必须实现抽象类 ...

  9. tornado超时机制

    1.https://blog.lzhaohao.info/archive/build-a-comet-application-using-tornado-and-nginx/ 2.http://qin ...

  10. 用StringBuilder和StringBuffer实现的Unicode解码方法的比较(Java)

    初衷是用正则来写一个Unicode字符串转码的方法,一开始是打算结合StringBuilder写的,但是看到jdk7的Matcher.appendReplacement文档中一段示例代码用了Match ...