C++11新特性应用--介绍几个新增的便利算法(用于排序的几个算法)
2016-01-20 22:11
483 查看
继续C++11在头文件algorithm中增加的算法。
至少我觉得,在stl的算法中,用到最多的就是sort了,我们不去探索sort的源码,就是介绍C++11新增的几个关于排序的函数。
对于一个序列,我们怎么知道他是不是有序的呢?这就用到了:
is_sorted
原型:
作用:
排队[first, last)是否有序。
这里需要注意的是,有两种原型。
第一种是默认的,即只有两个参数,这样是判断[first, last)是否按升序排列,即<。
第二种是三个参数的,这样是判断[first, last)区间是否按comp进行排序的。
应用:
这里用到了一个全排列算法,不是C++11新增的内容,就不再赘述。
上面的代码展示了两种使用is_sorted的version。
另一一点需要注意的是:
如果范围内的元素个数少于两个,总是返回true.
is_sorted_until
原型:
作用:
Find first unsorted element in range
Returns an iterator to the first element in the range [first,last) which does not follow an ascending order.
If the entire range is sorted, the function returns last.
应用:
至少我觉得,在stl的算法中,用到最多的就是sort了,我们不去探索sort的源码,就是介绍C++11新增的几个关于排序的函数。
对于一个序列,我们怎么知道他是不是有序的呢?这就用到了:
is_sorted
原型:
template <class ForwardIterator> ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last); template <class ForwardIterator, class Compare> ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last, Compare comp);
作用:
排队[first, last)是否有序。
这里需要注意的是,有两种原型。
第一种是默认的,即只有两个参数,这样是判断[first, last)是否按升序排列,即<。
第二种是三个参数的,这样是判断[first, last)区间是否按comp进行排序的。
应用:
#include <iostream> // std::cout #include <iostream> // std::cout #include <algorithm> // std::is_sorted, std::prev_permutation #include <array> // std::array bool compare(int a, int b) { return a>b; //升序排列,如果改为return a>b,则为降序 } int main() { std::array<int, 4> foo{ 2,4,1,3 }; std::array<int, 4> foo2{ 2,4,1,3 }; do { // try a new permutation: std::prev_permutation(foo.begin(), foo.end()); // print range: std::cout << "foo:"; for (int& x : foo) std::cout << ' ' << x; std::cout << '\n'; } while (!std::is_sorted(foo.begin(), foo.end())); std::cout << "the range is sorted!\n"; do { // try a new permutation: std::prev_permutation(foo2.begin(), foo2.end()); // print range: std::cout << "foo2:"; for (int& x : foo2) std::cout << ' ' << x; std::cout << '\n'; } while (!std::is_sorted(foo2.begin(), foo2.end(), compare)); std::cout << "the range is Descending sorted!\n"; return 0; } //输出: // foo: 2 3 4 1 // foo : 2 3 1 4 // foo : 2 1 4 3 // foo : 2 1 3 4 // foo : 1 4 3 2 // foo : 1 4 2 3 // foo : 1 3 4 2 // foo : 1 3 2 4 // foo : 1 2 4 3 // foo : 1 2 3 4 // the range is sorted! // foo2 : 2 3 4 1 // foo2 : 2 3 1 4 // foo2 : 2 1 4 3 // foo2 : 2 1 3 4 // foo2 : 1 4 3 2 // foo2 : 1 4 2 3 // foo2 : 1 3 4 2 // foo2 : 1 3 2 4 // foo2 : 1 2 4 3 // foo2 : 1 2 3 4 // foo2 : 4 3 2 1 // the range is Descending sorted!
这里用到了一个全排列算法,不是C++11新增的内容,就不再赘述。
上面的代码展示了两种使用is_sorted的version。
另一一点需要注意的是:
如果范围内的元素个数少于两个,总是返回true.
is_sorted_until
原型:
template <class ForwardIterator> ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last); template <class ForwardIterator, class Compare> ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last, Compare comp);
作用:
Find first unsorted element in range
Returns an iterator to the first element in the range [first,last) which does not follow an ascending order.
If the entire range is sorted, the function returns last.
应用:
#include <iostream> // std::cout #include <algorithm> // std::is_sorted_until, std::prev_permutation #include <array> // std::array int main () { std::array<int,4> foo {2,4,1,3}; std::array<int,4>::iterator it; do { // try a new permutation: std::prev_permutation(foo.begin(),foo.end()); // print range: std::cout << "foo:"; for (int& x:foo) std::cout << ' ' << x; it=std::is_sorted_until(foo.begin(),foo.end()); std::cout << " (" << (it-foo.begin()) << " elements sorted)\n"; } while (it!=foo.end()); std::cout << "the range is sorted!\n"; return 0; }
相关文章推荐
- C++11新特性应用--介绍几个新增的便利算法(用于排序的几个算法)
- c语言指针传递说明
- 最大子列和问题
- 关于c语言中负数位移位操作的漫谈
- C++的访问控制符与多态之间的关系
- C++开发人脸性别识别教程(14)——灰度图像识别BUG处理
- sys/param.h 和 limits.h头文件说明
- c++中new和malloc的区别
- c语言成绩统计系统
- C++迭代器
- C和C++里面常见错误和异常出现怎么解决总结(不断更新)
- C++ 现代编程风格速查表
- 遗传算法与C++实现
- C++11异常处理 noexcept
- C++中重载、重写(覆盖)和隐藏的区别实例分析
- 【C++】 复数类操作
- C语言开发函数库时对外接口隐藏库内结构体实现细节的方法
- C++:模板函数2(多个数的和)
- C/C++中extern关键字详解
- C++:模板函数1(2个数的和)