++


partial_sort_copy()


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

partial_sort_copy() , partial_sort(), , [result_first,result_last] ( , ). , :

int ia[] = {29,23,20,22,17,15,26,51,19,12,35,40};

int ia2[5];

partial_sort_copy(), middle :

partial_sort_copy( &ia[0], &ia[7], &ia[12],

&ia2[0], &ia2[5] );

ia2 : {12,15,17,19,20}. .

#include <algorithm>

#include <vector>

#include <iostream.h>

/*

* :

: 69 23 80 42 17 15 26 51 19 12 35 8

partial_sort() :

8 12 15 17 19 23 26 80 69 51 42 35

partial_sort_copy()

26 23 19 17 15 12 8

*/

int main()

{

int ia[] = { 69,23,80,42,17,15,26,51,19,12,35,8 };

vector< int,allocator > vec( ia, ia+12 );

ostream_iterator<int> out( cout," " );

cout << " : ";

copy( vec.begin(), vec.end(), out ); cout << endl;

 

cout << " partial_sort() : "

<< " \n";

partial_sort( vec.begin(), vec.begin()+7, vec.end() );

copy( vec.begin(), vec.end(), out ); cout << endl;

 

vector< int, allocator > res(7);

cout << " partial_sort_copy() \n\t"

<< " \n";

partial_sort_copy( vec.begin(), vec.begin()+7, res.begin(),

res.end(), greater<int>() );

copy( res.begin(), res.end(), out ); cout << endl;

}




- -  - -  - -