-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstd_sort_examples.cc
66 lines (60 loc) · 1.82 KB
/
std_sort_examples.cc
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
// std::sort
void test_sort()
{
std::cout << "----------std::sort----------" << std::endl;
std::vector<int> v{3, 1, 4, 1, 5, 9};
std::sort(v.begin(), v.end());
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
// std::stable_sort
void test_stable_sort()
{
std::cout << "----------std::stable_sort----------" << std::endl;
std::vector<int> v{3, 1, 4, 1, 5, 9};
std::stable_sort(v.begin(), v.end());
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
// std::partial_sort
void test_partial_sort()
{
std::cout << "----------std::partial_sort----------" << std::endl;
std::vector<int> v{3, 1, 4, 1, 5, 9};
std::partial_sort(v.begin(), v.begin() + 3, v.end());
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
// std::partial_sort_copy
void test_partial_sort_copy()
{
std::cout << "----------std::partial_sort_copy----------" << std::endl;
std::vector<int> v{3, 1, 4, 1, 5, 9};
std::vector<int> v2(3);
std::partial_sort_copy(v.begin(), v.end(), v2.begin(), v2.end());
std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
// std::sort_heap
void test_sort_heap()
{
std::cout << "----------std::sort_heap----------" << std::endl;
std::vector<int> v{3, 1, 4, 1, 5, 9};
std::make_heap(v.begin(), v.end());
std::sort_heap(v.begin(), v.end());
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
auto main() -> int
{
test_sort();
test_stable_sort();
test_partial_sort();
test_partial_sort_copy();
test_sort_heap();
return 0;
}