-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlambdas.cc
More file actions
61 lines (47 loc) · 1.33 KB
/
Copy pathlambdas.cc
File metadata and controls
61 lines (47 loc) · 1.33 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
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>
using std::vector;
using std::string;
using std::cout;
using std::endl;
int main()
{
vector<std::string> v{"9", "12", "13", "14", "18", "42", "75"};
string prefix("hi ");
for_each(v.begin(), v.end(), [&prefix](const auto& a) {
cout << prefix + a << endl;
});
auto print = [](const vector<std::string>& c) {
for(const auto& a : c)
cout << a << endl;
};
std::function<void(const vector<std::string>&)> stored=print;
cout<<"Starting order: "<<endl;
print(v);
cout<<typeid(print).name()<<endl;
cout<<typeid(stored).name()<<endl;
cout<<"Ascii sort: "<<endl;
std::sort(v.begin(), v.end());
print(v);
cout<<"Numerical sort: "<<endl;
std::sort(v.begin(), v.end(), [](const std::string& a, const std::string& b)
{
return atoi(a.c_str()) < atoi(b.c_str());
});
print(v);
cout<<"Absolute sort"<<endl;
std::vector<int> v2{3, -1, -4, 1, -5, -9, -2, 6, -5};
qsort(&v2[0], v2.size(), sizeof(int), [](const void *a, const void* b)
{
if(abs(*(int*)a) < abs(*(int*)b))
return -1;
if(abs(*(int*)a) > abs(*(int*)b))
return 1;
return 0;
});
for(const auto& val : v2)
cout<<val<<endl;
}