algorithm库介绍之---- stable_sort()方法 与 sort()方法

bool less_len(const string &str1, const string &str2)
{
return str1.length() < str2.length();
}

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

bool comp_as_int(double i, double j)
{
return (int(i)<int(j));
}

int main()
{
double mydoubles[] = {3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58};
vector<double> v;
vector<double>::iterator it;

v.assign(mydoubles, mydoubles + 8);

cout<<"use default comparison:"<<endl;
stable_sort(v.begin(), v.end());

for(it = v.begin(); it != v.end(); it++)
cout<<*it<<" ";
cout<<endl;

cout<<"use selfdefined comparison function comp_as_int():"<<endl;
v.assign(mydoubles, mydoubles + 8);
stable_sort(v.begin(), v.end(), comp_as_int);

for(it = v.begin(); it != v.end(); it++)
cout<<*it<<" ";
cout<<endl;
cout<<"if it is not sorted with stable_sort(), the sequence of all elements between 1 and 2 will be set randomly..."<<endl;

return 0;
}

1 use default comparison:
2 1.32 1.41 1.62 1.73 2.58 2.72 3.14 4.67
3 use selfdefined comparison function comp_as_int():
4 1.41 1.73 1.32 1.62 2.72 2.58 3.14 4.67
5 if it is not sorted with stable_sort(), the sequence of all elements between 1 and 2 will be set randomly...

STL stable_sort 稳定排序

2013-11-13 10:26:34

STL之stable_sort与sort

2015-04-10 17:57:03

stable_sort()与sort()的用法区别

2017-02-07 16:42:17

【stable_sort稳定排序】

2011-08-02 02:55:07

sort和stable_sort的区别和联系

2017-01-23 10:40:19

stable_sort() 和 sort() 的区别

2014-04-28 20:15:22

C++ sort(),stable_sort()和partial_sort()

2015-07-29 12:15:23

第一次用 stable_sort

2017-10-20 19:52:44

STL_算法_对所有元素排序(sort、stable_sort)

2016-01-28 19:41:54

C++排序之stable_sort()的方法

2017-02-06 13:23:51