Skip to content

Commit bcb9b26

Browse files
authored
Functor method
1 parent 8d07e76 commit bcb9b26

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

cpp4.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
struct increment{
5+
6+
int operator()(int n) const{
7+
return n + 1;
8+
}
9+
};
10+
11+
12+
void my_objective(std::vector<int>& vec, const increment& f) {
13+
for (int i = 0; i < arr.size(); ++i)
14+
arr[i] = f(arr[i]);
15+
}
16+
17+
void print(const std::vector<int>& vec){
18+
for(const int n : vec)
19+
std::cout << n << "\n";
20+
std::cout << std::endl;
21+
}
22+
23+
24+
int main() {
25+
std::vector<int> vec = {0, 1, 2, 3, 4};
26+
const increment functor;
27+
28+
my_objective(vec, functor);
29+
30+
print(vec);
31+
32+
std::cin.get();
33+
34+
}

0 commit comments

Comments
 (0)