STL算法之sort和stable_sort
2017-03-15 09:35
405 查看
1.stable_sort 和 sort的区别在于 前者作排序可以使原来的"相同"的值在序列中的相对位置不变
如 1 4 6 7 4' (4 和 4'值相等,加上' 表示是2个元素)
那么stable_sort能保证排序完 4 仍然在4' 前 也就是输出1 4 4' 6 7;但是sort 没有这个功能,算法不能保证这一点
2.在标准算法<algorithm>中的一部分算法 如果这个算法默认使用的是 < 运算符,那么这类算法通常提供一个重载版本,这个重载版本包括3个参数,而不是2个.新增的第三个参数是一个函数指针,这个函数指针指向的函数 就是用来代替默认的 < 运算符的.
也就是说如果容器中的2个元素 ,元素a和元素b,输入个这个新增的比较函数之后,函数返回true,那么a将会放在b前面,LZ给的例子里面这个比较函数能够用使长度大的排在前面.
同时,使用了stable_sort能够保证2个长度相同的元素按原顺序排列
//大部分容器适用、不适用于list容器
sort(b,e)
sort(b,e,p)
stable_sort(b,e)
stable_sort(b,e,p)
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
using namespace std;
/*****************************************
//大部分容器适用、不适用于list容器
sort(b,e)
sort(b,e,p)
stable_sort(b,e)
stable_sort(b,e,p)
*****************************************/
/**----------------------------------------------------------------------------------
注意:不适用于list容器,list有成员函数sort()
----------------------------------------------------------------------------------**/
/*************************************************************************************
std::sort 所有排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class RandomAccessIterator>
void sort ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
//eg:
*************************************************************************************/
/*************************************************************************************
std::stable_sort 所有排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class RandomAccessIterator>
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
//eg:
*************************************************************************************/
bool myfunction (int i,int j)
{
return (i<j);
}
struct myclass
{
bool operator() (int i,int j)
{
return (i<j);
}
} myobject;
bool compare_as_ints (double i,double j)
{
return (int(i)<int(j));
}
int main ()
{
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
vector<int>::iterator it;
// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
// using object as comp
sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)
// print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;
cout << endl;
/**---------------------------------------------------------------------------------------**/
double mydoubles[] = {3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58};
deque<double> mydeque;
deque<double>::iterator id;
mydeque.assign(mydoubles,mydoubles+8);
cout << "using default comparison:";
stable_sort (mydeque.begin(), mydeque.end());
for (id=mydeque.begin(); id!=mydeque.end(); ++id)
cout << " " << *id;
mydeque.assign(mydoubles,mydoubles+8);
cout << "\nusing 'compare_as_ints' :";
stable_sort (mydeque.begin(), mydeque.end(), compare_as_ints);
for (id=mydeque.begin(); id!=mydeque.end(); ++id)
cout << " " << *id;
cout << endl;
return 0;
}
[cpp] view
plain copy
myvector contains: 12 26 32 33 45 53 71 80
using default comparison: 1.32 1.41 1.62 1.73 2.58 2.72 3.14 4.67
using 'compare_as_ints' : 1.41 1.73 1.32 1.62 2.72 2.58 3.14 4.67
如 1 4 6 7 4' (4 和 4'值相等,加上' 表示是2个元素)
那么stable_sort能保证排序完 4 仍然在4' 前 也就是输出1 4 4' 6 7;但是sort 没有这个功能,算法不能保证这一点
2.在标准算法<algorithm>中的一部分算法 如果这个算法默认使用的是 < 运算符,那么这类算法通常提供一个重载版本,这个重载版本包括3个参数,而不是2个.新增的第三个参数是一个函数指针,这个函数指针指向的函数 就是用来代替默认的 < 运算符的.
也就是说如果容器中的2个元素 ,元素a和元素b,输入个这个新增的比较函数之后,函数返回true,那么a将会放在b前面,LZ给的例子里面这个比较函数能够用使长度大的排在前面.
同时,使用了stable_sort能够保证2个长度相同的元素按原顺序排列
sort(b,e)
sort(b,e,p)
stable_sort(b,e)
stable_sort(b,e,p)
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
using namespace std;
/*****************************************
//大部分容器适用、不适用于list容器
sort(b,e)
sort(b,e,p)
stable_sort(b,e)
stable_sort(b,e,p)
*****************************************/
/**----------------------------------------------------------------------------------
注意:不适用于list容器,list有成员函数sort()
----------------------------------------------------------------------------------**/
/*************************************************************************************
std::sort 所有排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class RandomAccessIterator>
void sort ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
//eg:
*************************************************************************************/
/*************************************************************************************
std::stable_sort 所有排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class RandomAccessIterator>
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
//eg:
*************************************************************************************/
bool myfunction (int i,int j)
{
return (i<j);
}
struct myclass
{
bool operator() (int i,int j)
{
return (i<j);
}
} myobject;
bool compare_as_ints (double i,double j)
{
return (int(i)<int(j));
}
int main ()
{
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
vector<int>::iterator it;
// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
// using object as comp
sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)
// print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;
cout << endl;
/**---------------------------------------------------------------------------------------**/
double mydoubles[] = {3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58};
deque<double> mydeque;
deque<double>::iterator id;
mydeque.assign(mydoubles,mydoubles+8);
cout << "using default comparison:";
stable_sort (mydeque.begin(), mydeque.end());
for (id=mydeque.begin(); id!=mydeque.end(); ++id)
cout << " " << *id;
mydeque.assign(mydoubles,mydoubles+8);
cout << "\nusing 'compare_as_ints' :";
stable_sort (mydeque.begin(), mydeque.end(), compare_as_ints);
for (id=mydeque.begin(); id!=mydeque.end(); ++id)
cout << " " << *id;
cout << endl;
return 0;
}
[cpp] view
plain copy
myvector contains: 12 26 32 33 45 53 71 80
using default comparison: 1.32 1.41 1.62 1.73 2.58 2.72 3.14 4.67
using 'compare_as_ints' : 1.41 1.73 1.32 1.62 2.72 2.58 3.14 4.67
![](http://static.blog.csdn.net/images/save_snippets.png)
相关文章推荐
- STL_算法_对所有元素排序(sort、stable_sort)
- STL_算法_对全部元素排序(sort、stable_sort)
- STL_算法(27)_对所有元素排序sort(b, e) sort(b, e, p) stable_sort(b, e) stable_sort(b, e, p)
- 算法学习 - STL的排序函数(sort)使用
- STL中的排序算法 sort stable_sort patition_sort等
- STL 之 stable_sort 将牌从大到小排序
- 【STL】Heap算法——push_heap、pop_heap、sort_heap、make_heap
- STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation
- 使用STL中的泛型算法sort
- STL源码剖析——STL算法之sort排序算法
- STL源码系列--List::sort算法解析
- 从零开始学C++之STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)
- STL源码:算法sort
- STL 源码剖析 算法 stl_algo.h -- partial_sort / partial_sort_copy
- STL中泛型算法find_if()与sort()的应用举例及详解
- STL_算法(26)_重排和分区 random_shuffle() ;partition() ;stable_partion()
- STL 源代码剖析 算法 stl_algo.h -- partial_sort / partial_sort_copy
- 关于利用STL中的sort算法对向量等容器进行排序的问题
- stl-stable_sort源码学习笔记
- STL学习笔记之sort算法