-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp14_regular_lambda.cpp
More file actions
91 lines (77 loc) · 2.27 KB
/
cpp14_regular_lambda.cpp
File metadata and controls
91 lines (77 loc) · 2.27 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// cpp14_regular_lambda.cpp
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <memory>
void print_line() { printf("----------------------------------------------------------\n"); return; }
// Using Regular lambda at sort
class Students {
public:
Students() {
add("one1111111"); add("two22222"); add("three3");
print_line(); print(); print_line();
sort();
print_line(); print(); print_line();
}
private:
std::vector<std::string> _s_list;
void add(std::string name) { _s_list.push_back(name); }
void print() { for ( auto n : _s_list) printf("%s\n", n.c_str()); }
void sort() {
std::sort(_s_list.begin(), _s_list.end(),
[](std::string const& a, std::string const& b){
return a.length() < b.length();
}
);
}
};
// Using Generic lambda at sort - C++14
class Names {
public:
Names() {
add("one1111111"); add("two22222"); add("three3");
print_line(); print(); print_line();
sort();
print_line(); print(); print_line();
}
private:
std::vector<std::string> _s_list;
void add(std::string name) { _s_list.push_back(name); }
void print() { for ( auto n : _s_list) printf("%s\n", n.c_str()); }
void sort() {
std::sort(_s_list.begin(), _s_list.end(),
[](auto a, auto b){
return a.length() < b.length();
}
);
}
};
void test_1() {
std::vector<std::string> names { "jknjkdn", "njkfvjknh", "jknbhvfjksb", "regbh"};
auto srt = [names]() mutable {
std::sort(names.begin(), names.end());
};
auto print = [names]() {
for ( auto n : names) printf("%s\n", n.c_str());
};
print_line(); print(); print_line();
srt();
print_line(); print(); print_line();
}
void test_2() {
auto p = std::make_unique<int>(10);
// The following line will cause error . But subsequent line works with 'init-capture' feature of C++14 lambda
// auto lambda = []() {
auto lambda = [p = std::move(p)]() {
printf("Value inside lambda => %d\n", *p);
};
lambda();
}
int main() {
Students s1;
Names n1;
test_1();
test_2();
return 0;
}