-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlambdaFunction.cpp
43 lines (32 loc) · 1.09 KB
/
lambdaFunction.cpp
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
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
bool lessLength(const std::string& f, const std::string& s){
return f.length() < s.length();
}
class GreaterLength{
public:
bool operator()(const std::string& f, const std::string& s) const{
return f.length() > s.length();
}
};
int main(){
// initializing with a initializer lists
std::vector<std::string> myStrVec = {"12345", "123456", "1234", "1", "12", "123", "12345"};
std::cout << "\n";
// sorting with the function
std::sort(myStrVec.begin(), myStrVec.end(), lessLength);
for (auto s: myStrVec) std::cout << s << " ";
std::cout << "\n";
// sorting with the function object
std::sort(myStrVec.begin(), myStrVec.end(), GreaterLength());
for (auto s: myStrVec) std::cout << s << " ";
std::cout << "\n";
// sorting with the lambda function
std::sort(myStrVec.begin(), myStrVec.end(), [](const std::string& f, const std::string& s){return f.length() < s.length();});
for (auto s: myStrVec) std::cout << s << " ";
std::cout << "\n";
std::cout << "\n\n";
}