首页 > 代码库 > STL_算法_局部排序(partial_sort、partial_sort_copy)

STL_算法_局部排序(partial_sort、partial_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_sort、partial_sort_copy)