-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec_sorting.cpp
More file actions
61 lines (46 loc) · 1.8 KB
/
Copy pathvec_sorting.cpp
File metadata and controls
61 lines (46 loc) · 1.8 KB
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
/*
~$ g++ vec_sorting.cpp -o vec_sorting
*/
#include <iostream> // std::cout
#include <algorithm> // std::generate, std::sort
#include <ctime> // std::time
#include <vector> // std::vector
#include <cstdlib> // std::rand, std::srand
using std::cout;
// function generator:
int RandomNumber() { return (std::rand()%100); }
bool sortAsc(int i,int j) { return (i<j); } // i<j ascending
bool sortDesc(int i,int j) { return (i>j); } // i>j descending
int main() {
// generate a vector randomly
std::srand(97);
std::vector<int> aVec (8);
std::generate(aVec.begin(), aVec.end(), RandomNumber);
cout << "unsorted vector:";
for (std::vector<int>::iterator it=aVec.begin(); it!=aVec.end(); ++it)
cout << ' ' << *it;
cout << '\n';
// make three copies of the vector that we will sort using two different comparators
std::vector<int> aVec1(aVec);
std::vector<int> aVec2(aVec);
std::vector<int> aVec3(aVec);
// default sort entire vector ascending
std::sort(aVec1.begin(), aVec1.end());
// use our comparator function sortDesc()
std::sort(aVec2.begin(), aVec2.end(), sortDesc);
// sort half the vector using sortAsc()
std::sort(aVec3.begin(), aVec3.begin()+4, sortAsc);
cout << "sorted using default ascending comparator:";
for (std::vector<int>::iterator it=aVec1.begin(); it!=aVec1.end(); ++it)
cout << ' ' << *it;
cout << '\n';
cout << "sorted using descending comparator:";
for (std::vector<int>::iterator it=aVec2.begin(); it!=aVec2.end(); ++it)
cout << ' ' << *it;
cout << '\n';
cout << "sorted 1st half of vector using ascending comparator:";
for (std::vector<int>::iterator it=aVec3.begin(); it!=aVec3.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}