STL_算法_局部排序(partial_sortpartial_sort_copy)

Posted 寻找星空的孩子

tags:

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

C++ Primer 学习中。。。

 

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


/*****************************************
//
partial_sort(b,se,e)
partial_sort(b,se,e,p)
partial_sort_copy(sb,se,db,de)
partial_sort_copy(sb,se,db,de,p)
*****************************************/
/**----------------------------------------------------------------------------------
STL算法---排序算法
sort()                  make_heap()
stable_sort()           push_heap()
partial_sort()          pop_heap()
partial_sort_copy()     sort_heap()
nth_element()
partition()
stable_partition()
----------------------------------------------------------------------------------**/


/**------http://blog.csdn.net/u010579068------**/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
using namespace std;

/*****************************************
//
partial_sort(b,se,e)
partial_sort(b,se,e,p)
partial_sort_copy(sb,se,db,de)
partial_sort_copy(sb,se,db,de,p)
*****************************************/
/**----------------------------------------------------------------------------------
STL算法---排序算法
sort()                  make_heap()
stable_sort()           push_heap()
partial_sort()          pop_heap()
partial_sort_copy()     sort_heap()
nth_element()
partition()
stable_partition()
----------------------------------------------------------------------------------**/
/*************************************************************************************
std::partial_sort                   所有排序容器适用                       algorithm
--------------------------------------------------------------------------------------
template <class RandomAccessIterator>
  void partial_sort ( RandomAccessIterator first, RandomAccessIterator middle,
                      RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
  void partial_sort ( RandomAccessIterator first, RandomAccessIterator middle,
                      RandomAccessIterator last, Compare comp );
//eg:

*************************************************************************************/

/*************************************************************************************
std::partial_sort_copy                   所有排序容器适用                   algorithm
--------------------------------------------------------------------------------------
template <class InputIterator, class RandomAccessIterator>
  RandomAccessIterator
    partial_sort_copy ( InputIterator first,InputIterator last,
                        RandomAccessIterator result_first,
                        RandomAccessIterator result_last );

template <class InputIterator, class RandomAccessIterator, class Compare>
  RandomAccessIterator
    partial_sort_copy ( InputIterator first,InputIterator last,
                        RandomAccessIterator result_first,
                        RandomAccessIterator result_last, Compare comp );
//eg:

*************************************************************************************/
bool myfunction (int i,int j)
{
    return (i<j);
}
template <typename T>
void Print(T& V)
{
    typename T::iterator iter=V.begin();
    while(iter != V.end())
    {
        cout<<*iter++<<" ";
    }
    cout<<endl;
}
int main ()
{
    int myints[] = {7,6,9,4,1,5,8,2,3};
    vector<int> myvector (myints, myints+9);
//    vector<int>::iterator it;

    // using default comparison (operator <):
    partial_sort (myvector.begin(), myvector.begin()+5, myvector.end());
    cout << "myvector contains:";
    Print(myvector);

    deque<int> mydeque(myints,myints+9);
    // using function as comp
    partial_sort (mydeque.begin(), mydeque.begin()+5, mydeque.end(),myfunction);

    // print out content:
    cout << "mydeque  contains:";
    Print(mydeque);
//    for (it=myvector.begin(); it!=myvector.end(); ++it)
//        cout << " " << *it;

    cout << endl;
    /**--------------------------------------------------------------------------**/
    vector<int> vec (5);
    deque <int> deq (5);

    // using default comparison (operator <):
    partial_sort_copy (myints, myints+9, vec.begin(), vec.end());
    cout << "myvector contains:";
    Print(vec);

    // using function as comp
    partial_sort_copy (myints, myints+9, deq.begin(), deq.end(), myfunction);
    // print out content:
    cout << "mydeque  contains:";
    Print(deq);
//    for (it=myvector.begin(); it!=myvector.end(); ++it)
//        cout << " " << *it;
    cout << endl;

    return 0;
}


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

STL_算法_局部排序(partial_sortpartial_sort_copy)

STL_算法_01_排序算法

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

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

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

STL_算法_对所有元素排序(sortstable_sort)