关于 stable_sort
2017-02-17 23:55
141 查看
你发现有sort和stable_sort,还有 partition 和stable_partition, 感到奇怪吧。其中的区别是,带有stable的函数可保证相等元素的原本相对次序在排序后保持不变。或许你会问,既然相等,你还管他相对位置呢,也分不清 楚谁是谁了?这里需要弄清楚一个问题,这里的相等,是指你提供的函数表示两个元素相等,并不一定是一摸一样的元素。
例如,如果你写一个比较函数:
此时,“apple” 和 “winter” 就是相等的,如果在”apple” 出现在”winter”前面,用带stable的函数排序后,他们的次序一定不变,如果你使用的是不带”stable”的函数排序,那么排序完 后,”Winter”有可能在”apple”的前面。
举例说明:
[cpp] view plain copy print?<strong>#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;
}</strong>
关于stable_sort()和sort()的区别:
今天遇到个问题,关于sort函数的.之前一直以为,sort是不稳定的,但是添加了比较函数的sort是稳定的,然而今天发现这个观点是错的.下面纯粹转载别人的博客,用于记录你发现有sort和stable_sort,还有 partition 和stable_partition, 感到奇怪吧。其中的区别是,带有stable的函数可保证相等元素的原本相对次序在排序后保持不变。或许你会问,既然相等,你还管他相对位置呢,也分不清 楚谁是谁了?这里需要弄清楚一个问题,这里的相等,是指你提供的函数表示两个元素相等,并不一定是一摸一样的元素。
例如,如果你写一个比较函数:
bool less_len(const string &str1, const string &str2) { return str1.length() < str2.length(); }
此时,“apple” 和 “winter” 就是相等的,如果在”apple” 出现在”winter”前面,用带stable的函数排序后,他们的次序一定不变,如果你使用的是不带”stable”的函数排序,那么排序完 后,”Winter”有可能在”apple”的前面。
举例说明:
[cpp] view plain copy print?<strong>#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;
}</strong>
<strong>#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; }</strong>输出结果:
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...
相关文章推荐
- 关于sort与stable_sort的初级用法
- 关于VS2008/2010中SORT,stable_sort的比较函数中strict weak ordering
- 关于perl中sort的用法举例
- stable_sort
- shell中关于sort的-o选项
- stable_sort()和sort()的区别
- stl-stable_sort源码学习笔记
- 关于STLsort double类型数据排序
- 关于sort排序通用的函数接口
- stable_sort()
- HDOJ 1106 排序 [stable_sort]
- STL之stable_sort与sort
- 关于版本号:alpha、beta、rc、stable
- 关于sort()的疑问?
- 关于list.sort()中对象的属性无法调用compareTo()方法解决方案
- LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告
- 关于array.sort(array,array)
- 网上关于sort结构体排序都不完整,我来写一个完整版的
- 关于sort
- stable_sort instance