STL_算法_局部排序(partial_sort、partial_sort_copy)

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

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;
}


标签: 代码

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:java 封装的基类Springjdbc可以通用

下一篇:java Struts2和Freemarker整合应用静态页面