forked from AayushGupta988/CPPstls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_inbuilt.cpp
More file actions
52 lines (41 loc) · 1.36 KB
/
sort_inbuilt.cpp
File metadata and controls
52 lines (41 loc) · 1.36 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
// #include<iostream>
// #include<vector>
// #include<algorithm>
// using namespace std;
// int main()
// {
// vector<int> a{2, 1, 5, 3, 0, -5, 4, 12, 22};
// sort(a.begin(), a.end());
// for (int i = 0; i < a.size(); i++)
// {
// cout << a[i] << " ";
// }
// cout << endl;
// return 0;
// }
// sort algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
bool myfunction(int i, int j) { return (i < j); }
struct myclass
{
bool operator()(int i, int j) { return (i < j); }
} myobject;
int main()
{
int myints[] = {32, 71, 12, 45, 26, 80, 53, 33};
std::vector<int> 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<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}