STL_算法_对全部元素排序(sortstable_sort)

Posted zhchoutai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STL_算法_对全部元素排序(sortstable_sort)相关的知识,希望对你有一定的参考价值。

C++ Primer 学习中。

?

简单记录下我的学习过程?(代码为主)


//大部分容器适用、不适用于list容器
sort(b,e)
sort(b,e,p)
stable_sort(b,e)
stable_sort(b,e,p)


/**------http://blog.csdn.net/u010579068------**/
#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;
}


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




以上是关于STL_算法_对全部元素排序(sortstable_sort)的主要内容,如果未能解决你的问题,请参考以下文章

STL_算法_依据第n个元素排序(nth_element)

STL_算法_根据第n个元素排序(nth_element)

STL_算法_Heap算法(堆排)(精)

STL_算法_01_排序算法

STL_算法_查找算法(lower_boundupper_boundequal_range)

STL_算法_06_遍历算法