C++11新特性应用--介绍几个新增的便利算法(不更改容器中元素顺序的算法)
总所周知。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新特性应用--介绍几个新增的便利算法(不更改容器中元素顺序的算法)的更多相关文章
- C++11新特性应用--介绍几个新增的便利算法(用于分区的几个算法)
今天继续. C++11新增的关于Non-modifying sequence operations和Modifying sequence operations的算法已经写了.具体信息见之前的博客. 以 ...
- C++11新特性应用--介绍几个新增的便利算法(用于排序的几个算法)
继续C++11在头文件algorithm中添加的算法. 至少我认为,在stl的算法中,用到最多的就是sort了,我们不去探索sort的源代码.就是介绍C++11新增的几个关于排序的函数. 对于一个序列 ...
- Java 11 新特性介绍
Java 11 已于 2018 年 9 月 25 日正式发布,之前在Java 10 新特性介绍中介绍过,为了加快的版本迭代.跟进社区反馈,Java 的版本发布周期调整为每六个月一次——即每半年发布一个 ...
- C++11新特性总结 (二)
1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...
- [转载] C++11新特性
C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...
- C++11 新特性之智能指针(shared_ptr, unique_ptr, weak_ptr)
这是C++11新特性介绍的第五部分,涉及到智能指针的相关内容(shared_ptr, unique_ptr, weak_ptr). shared_ptr shared_ptr 基本用法 shared_ ...
- 【C++11新特性】 C++11智能指针之weak_ptr
如题,我们今天要讲的是C++11引入的三种智能指针中的最后一个:weak_ptr.在学习weak_ptr之前最好对shared_ptr有所了解.如果你还不知道shared_ptr是何物,可以看看我的另 ...
- Redis 6.0 新特性 ACL 介绍
Redis 6.0 新特性 ACL 介绍 Intro 在 Redis 6.0 中引入了 ACL(Access Control List) 的支持,在此前的版本中 Redis 中是没有用户的概念的,其实 ...
- c++ 11 线程池---完全使用c++ 11新特性
前言: 目前网上的c++线程池资源多是使用老版本或者使用系统接口实现,使用c++ 11新特性的不多,最近研究了一下,实现一个简单版本,可实现任意任意参数函数的调用以及获得返回值. 0 前置知识 首先介 ...
随机推荐
- PHP面向对象摘要
一.面向对象的三种特性,分别是封装性,继承性和多态性. 1.封装性:封装是面向对象的核心思想,将对象的属性和行为封装起来,不需要让外界知道具体的实现细节,这就是封装思想. 2.继承性:继承性主要是描述 ...
- 免费好用的阿里云云盾证书服务(https证书)申请步骤
推荐一个免费的阿里云产品:云盾证书(https证书) 为了能让非专业人士看懂,同样尽量用直白的话,一般来说:当你个人需要建立网站,或者公司要建立官网.商城,通常需要先购买服务器或云主机,虚拟空间,然后 ...
- 深度学习之tensorflow (一)
一.TensorFlow简介 1.TensorFlow定义: tensor :张量,N维数组 Flow : 流,基于数据流图的计算 TensorFlow : 张量从图像的一端流动到另一端的计算 ...
- 在64位Win7环境+64位JDK下,运行64位Eclipse,提示“Failed to load the JNI shared library”错误,提示jvm.dll不对
-startup plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar --launcher.library plugins/org.ecl ...
- javascript函数式编程(一)
一.引言 javascript函数式编程在最近两年来频繁的出现在大众的视野,越来越多的框架(react,angular,vue等)标榜自己使用了函数式编程的特性,好像一旦跟函数式编程沾边,就很高大上一 ...
- C#中四步轻松使用log4net记录本地日志
在这里,记录我在项目中使用log4net记录本地日志的步骤.在不会之前感觉很难,很神秘,一旦会了之后其实没那么难.其实所有的事情都是一样的,下面我就分享一下我使用log4Net的经验. 第一步:首先从 ...
- CentOS 6.4安装配置LNMP服务器(Nginx+PHP+MySQL)
一 安装篇 1. 安装nginx yum check-update #更新yum源 yum remove httpd* php* #删除系统自带的软件包 yum install nginx #安装ng ...
- java oop详解
近日来重温了一下java oop的知识.加深了对面向对象的理解.尤其时继承方面.故写一篇博客.记录一下自己的想法和心得 1.面向对象主要分为三大点(封装,继承,多态) 封装的思想促进了类的形成.相比于 ...
- HTML5将给企业带来什么?
HTML5是近几年来互联网行业的热门词汇,火热的厉害,未来的网络属于HTML5. 乔布斯生前也在公开信<Flash之我见>中预言:像HTML5这样在移动时代中创立的新标准,将会在移动设备上 ...
- Tomcat在修改代码后不会自动reload解决办法
1.找到使用的Tomcat安装目录conf配置文件夹下context.xml文件 2.找到<Context>节点,改为<Context reloadable="true&q ...