C++11新特性应用--介绍几个新增的便利算法(不更改容器中元素顺序的算法)
来源:互联网 发布:视频动态截图软件 编辑:程序博客网 时间:2024/05/16 07:28
总所周知,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::arrayint 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::arrayint 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::arrayint 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::arrayint 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新特性应用--介绍几个新增的便利算法(用于分区的几个算法)
- C++11新特性应用--介绍几个新增的便利算法(用于排序的几个算法)
- C++11新特性应用--介绍几个新增的便利算法(关于最大值和最小值的算法)
- C++11新特性应用--介绍几个新增的便利算法(stl中的heap使用,最大堆)
- c++11新增的一些便利的算法
- c++11新增的一些便利的算法
- 《数据结构、算法与应用》8.(顺序查找数组中第一个出现指定元素的位置)
- 介绍几个重要的算法
- 贪心算法的几个应用
- 贪心算法的几个应用
- 贪心算法的几个应用
- STL的几个算法应用
- 对容器元素重新排序的算法
- 关于 4.5中新增的异步编程新特性总结
- Android Studio 3.0 新特性,Android Studio 3.0新增的功能和更改
- C语言算法的特性
- 第11讲-算术运算符与算术表达式-练习
- 机器学习-非监督学习(Unüberwachte Lernverfahren)+大型数据库:Clustering-2
- HDU 2063 过山车
- [代码] 根据homework\homework.html文件,返回<学生ID,交作业顺序>的Map
- 内部类、匿名内部类、异常及异常处理
- C++11新特性应用--介绍几个新增的便利算法(不更改容器中元素顺序的算法)
- 蜗牛历险记(开篇)
- Caused by: java.lang.ClassNotFoundException: org.apache.shiro.spring.LifecycleBeanPostProcessor
- stringstream的很好的用法
- 【杭电oj】1717 - 小数化分数2(小数化分数,GCD)
- php单例模式
- vb.net——通过邮箱动态发送验证码实现完美注册
- 【网页设计】关于一个静态展示类网站的建立
- 控制器View是如何创建的?