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 前置知识 首先介 ...
随机推荐
- PTA 循环单链表区间删除 (15 分)
本题要求实现带头结点的循环单链表的创建和单链表的区间删除.L是一个带头结点的循环单链表,函数ListCreate_CL用于创建一个循环单链表,函数ListDelete_CL用于删除取值大于min小于m ...
- 面向亿万级用户的QQ一般做什么?——兴趣部落的Web同构直出分享
作者:李强,腾讯web开发工程师 商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. 原文链接:http://wetest.qq.com/lab/view/348.html 一.什么是同构 ...
- Python 解LeetCode:671. Second Minimum Node In a Binary Tree
题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...
- JavaScript 面试中常见算法问题详解
1.阐述下 JavaScript 中的变量提升 所谓提升,顾名思义即是 JavaScript 会将所有的声明提升到当前作用域的顶部.这也就意味着我们可以在某个变量声明前就使用该变量,不过虽然 Java ...
- js的call() ,apply() 两种方法的区别和用法,最白话文的解释,让枯燥滚粗!
百度了一圈calll()函数和apply()函数,感觉还是糊里糊涂 正好我前几天刚又重新翻了一遍 那本 600多页 的圣经书,我习惯时不时的去打下基础,只是为了用来装逼,给人讲解....(我是有多蛋疼 ...
- 在Linux机器上安装telnet命令
一.查看本机是否安装 telnet #rpm -qa | grep telnet 如果什么都不显示,说明没有安装telnet 二.开始安装 yum install xinetd y ...
- C++静态库与动态库(转)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0cAAAC6CAIAAADqPKGOAAAgAElEQVR4nO2dy5WrOhREFYgjcAQ9Iw
- shell 备份脚本
[root@izwz9hmoz58gvtu0ldpm0iz ~]# cat /usr/local/aaaa/shell_script/Mysql_Dump_LJY.sh #! /bin/bash to ...
- webpack 3.X学习之图片处理
CSS中图片处理 在src目录下新建一个images目录,把图片放入images文件夹中:在index.html文件中增加一个div标签: /src/index.html: <div id=&q ...
- python/numpy/tensorflow中,对矩阵行列操作,下标是怎么回事儿?
Python中的list/tuple,numpy中的ndarrray与tensorflow中的tensor. 用python中list/tuple理解,仅仅是从内存角度理解一个序列数据,而非数学中标量 ...