C++ 算法库(4) 排序操作(二)

2014-11-24 08:23:44 · 作者: · 浏览: 2
myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';

return 0;
}

输出:

1
myvector contains: 1 2 3 4 5 9 8 7 6


partial_sort_copy

拷贝部分排序的结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// partial_sort_copy example
#include // std::cout
#include // std::partial_sort_copy
#include // std::vector

bool myfunction (int i,int j) { return (i
int main () {
int myints[] = {9,8,7,6,5,4,3,2,1};
std::vector myvector (5);

// using default comparison (operator <):
std::partial_sort_copy (myints, myints+9, myvector.begin(), myvector.end());

// using function as comp
std::partial_sort_copy (myints, myints+9, myvector.begin(), myvector.end(), myfunction);

// print out content:
std::cout << "myvector contains:";
for (std::vector ::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';

return 0;
}

输出:

1
myvector contains: 1 2 3 4 5


sort

排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// sort algorithm example
#include // std::cout
#include // std::sort
#include // std::vector

bool myfunction (int i,int j) { return (i
struct myclass {
bool operator() (int i,int j) { return (i } myobject;

int main () {
int myints[] = {32,71,12,45,26,80,53,33};
std::vector myvector (myints, myints+8); // 32 71 12 45 26 80 53 33

// using default comparison (operator <):
std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33

// using function as comp
std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

// using object as comp
std::sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)

// print out content:
std::cout << "myvector contains:";
for (std::vector ::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';

return 0;
}

输出:

1
myvector contains: 12 26 32 33 45 53 71 80


stable_sort

稳定排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// stable_sort example
#include // std::cout
#include // std::stable_sort
#include // std::vector

bool compare_as_ints (double i,double j)
{
return (int(i) }

int main () {
double mydoubles[] = {3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58};

std::vector myvector;

myvector.assign(mydoubles,mydoubles+8);

std::cout << "using default comparison:";
std::stable_sort (myvector.begin(), myvector.end());
for (std::vector ::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';

myvector.assign(mydoubles,mydoubles+8);

std::cout << "using 'compare_as_ints' :";
std::stable_sort (myvector.begin(), myvector.end(), compare_as_ints);
for (std::vector ::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';

return 0;
}

输出:

1
2
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


特别说明:函数的中文释义来自:http://classfoo.cn/cpp/head/76573_319/,例子来自:http://www.cplusplus.com/reference/algorithm/