-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlambda_trivial.cpp
49 lines (40 loc) · 931 Bytes
/
lambda_trivial.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
44
45
46
47
48
49
#include <iostream>
#include <vector>
#include <algorithm>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
/* a lambda function that returns the sum of a vector */
auto sum = [](const auto& vec) -> auto {
auto s = 0.0;
for( auto& elem: vec) {
s += elem;
}
return s;
};
template<class T>
class Matrix {
public:
Matrix(vector<T>& vect) { setvec(vect); }
~Matrix() { vec.clear(); }
void setvec(vector<T>& source_vector) {
typename vector<T>::iterator start = source_vector.begin();
while(start != source_vector.end()) {
vec.push_back(*start);
start++;
}
}
/* vector<T> */ auto Getvec() const {
return vec;
}
private:
vector<T> vec;
};
int main()
{
vector<int> vec{1,2,3,4,5};
Matrix<int> m(vec);
cout << "The sum of vec elements is : " << sum(m.Getvec()) << endl;
return 0;
}