STL sort 的用法

sort的原型:

default (1)
template <class RandomAccessIterator>
  void sort (RandomAccessIterator first, RandomAccessIterator last);
custom (2)
template <class RandomAccessIterator, class Compare>
  void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

说明:

1、排序的区间可以是数组下标或者迭代器,如果是迭代器必须是随机迭代器(如vector支持的迭代器和自己定义的支持随机迭代器的数据结构)

2、默认情况下是采用从小到大的方式排列的

3、STL内部提供了less<T>() 、greater<T>(),以及自定义的数据结构:

string a[]={"1","3","2"};
vector<string> b(a,a+3);
    sort(b.begin(),b.end());
//sort(b.begin(),b.end(),less<string>()); the same

定义排序函数:

方法1:声明外部比较函数

bool Less(const Student& s1, const Student& s2)
{
    return s1.name < s2.name; //从小到大排序
}
std::sort(sutVector.begin(), stuVector.end(), Less);

注意:比较函数必须写在类外部(全局区域)或声明为静态函数

当comp作为类的成员函数时,默认拥有一个this指针,这样和sort函数所需要使用的排序函数类型不一样。

否则,会出现错误

class Leg
{
public:
    int length;
    int cost;
    Leg(int x,int y):length(x),cost(y){}
    bool operator < (const Leg& a) const //不加const会报错的
    {
       return length<a.length;
    }
    bool operator=(const Leg& a)const
    {
        return length==a.length;
    }
};

方法2:重载类的比较运算符

bool operator<(const Student& s1, const Student& s2)
{
    return s1.name < s2.name; //从小到大排序
}
std::sort(sutVector.begin(), stuVector.end());

方法3:声明比较类

struct Less
{
    bool operator()(const Student& s1, const Student& s2)
    {
        return s1.name < s2.name; //从小到大排序
    }
};

std::sort(sutVector.begin(), stuVector.end(), Less());
时间: 2024-08-26 14:12:08

STL sort 的用法的相关文章

STL的其他用法总结

2017-08-20 17:26:07 writer:pprp 1.adjacent_find() 下面是源码实现: template <class ForwardIterator> ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last) { if (first != last) { ForwardIterator next=first; ++next; while (next != last) {

C++ sort函数用法

参考文档:http://hi.baidu.com/posinfo/item/dc3e73584c535cc9d2e10c27 C++ sort函数用法 FROM:http://hi.baidu.com/blackdemonfish/blog/item/e2c1d655d702a45ed0090652%2Ehtml 最近算法作业经常需要排序.偶是一个很懒的人,于是一直用C++的sort进行排序---不少同志对此心存疑虑,所以今天就写一写sort的用法.声明:此用法是从某大牛的程序中看到的,其实偶只

stl sort分析

最近写代码,无意中发现了一个坑,关于自定义比较函数的stl sort函数的坑,于是记录下来. 先贴代码: 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5 struct finder 6 { 7 bool operator()(int first, int second){return first <= second;} 8 } my_finder; 9 10 int main

c++ STL sort struct comp

详细解说 STL 排序(Sort) http://www.cppblog.com/mzty/archive/2005/12/15/1770.html 详细解说 STL 排序(Sort) 作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1.1 所有sort算法介绍 1.2 sort 中的比较函数 1.3 sort 的稳定性 1.4 全排序 1.5 局部排序 1.6 nth_element 指定元素排序 1.7 partit

sort函数用法

头文件: #include <algorithm> using namespace std; 1.默认的sort函数是按升序排序. sort(a,a+n);                //两个参数分别为待排序数组的首地址和尾地址 2.可以自己写一个cmp函数,按特定意图进行排序. 例如 : 1).对数组a降序排序 int cmp( const int &a, const int &b ){ if( a > b ) return 1; else return 0; }

Perl Sort函数用法总结和使用实例

一) sort函数用法 sort LISTsort BLOCK LISTsort SUBNAME LIST sort的用法有如上3种形式.它对LIST进行排序,并返回排序后的列表.假如忽略了SUBNAME或BLOCK,sort按标准字串比较顺序来进行(例如ASCII顺序).如果指定了SUBNAME,它实际上是个子函数的名字,该子函数对比2个列表元素,并返回一个小于,等于,或大于0的整数,这依赖于元素以何种顺序来sort(升序,恒等,或降序).也可提供一个BLOCK作为匿名子函数来代替SUBNAM

STL sort()函数

C++之所以得到这么多人的喜欢,是因为它既具有面向对象的概念,又保持了C语言高效的特点.STL 排序算法同样需要保持高效.因此,对于不同的需求,STL提供的不同的函数,不同的函数,实现的算法又不尽相同. 1.1 所有sort算法介绍 所有的sort算法的参数都需要输入一个范围,[begin, end).这里使用的迭代器(iterator)都需是随机迭代器(RadomAccessIterator), 也就是说可以随机访问的迭代器,如:it+n什么的.(partition 和stable_parti

STL sort

STL的sort()算法,数据量大时采用Quick Sort,分段递归排序,一旦分段后的数据量小于某个门槛,为避免Quick Sort的递归调用带来过大的额外负荷,就改用Insertion Sort.如果递归层次过深,还会改用Heap Sort.本文先分别介绍这个三个Sort,再整合分析STL sort算法(以上三种算法的综合) -- Introspective Sorting(内省式排序). 一.Insertion Sort Insertion Sort是<算法导论>一开始就讨论的算法.它的

python 中 sorted() 和 list.sort() 的用法

今天用python自带的sorted对一个列表进行排序, 在这里总结一下 只要是可迭代对象都可以用sorted . sorted(itrearble, cmp=None, key=None, reverse=False) =号后面是默认值 默认是升序排序的, 如果想让结果降序排列,用reverse=True 最后会将排序的结果放到一个新的列表中, 而不是对iterable本身进行修改. eg: 1, 简单排序 sorted('123456')  字符串 ['1', '2', '3', '4',