您的位置:首页 > 编程语言 > C语言/C++

C++11新特性应用--介绍几个新增的便利算法(用于排序的几个算法)

2016-01-20 22:11 483 查看
继续C++11在头文件algorithm中增加的算法。

至少我觉得,在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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: