forked from furkankirac/cs321-2019-20-fall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek7-app1.cpp
57 lines (42 loc) · 1.09 KB
/
week7-app1.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
50
51
52
53
54
55
56
57
#include <iostream>
// function objects, lambda functions
// containers and iterator protocol/interface
// std::accumulate
template<typename T>
inline void swap_by_ptr(T* first, T* second)
{
auto temp = *first;
*first = *second;
*second = temp;
}
template<typename T>
inline void swap_by_ref(T& first, T& second)
{
auto temp = first;
first = second;
second = temp;
}
int func1(double a) { return (int)a; }
int func2(double a) { return (int)(a*2.5); }
//typedef int (*foo_ptr)(int);
using foo_ptr = int (*)(double);
int main(int argc, char* argv[])
{
// auto ptr = foo_ptr{};
foo_ptr ptr;
ptr = &func1;
ptr(5.0); // calls func1(5.0)
ptr(3.0); // calls func1(3.0)
ptr = &func2;
ptr(5.0); // calls func2(5.0)
auto i = 10, j = 20;
swap_by_ptr<int>(&i, &j);
std::cout << i << std::endl;
std::cout << j << std::endl;
swap_by_ref<int>(i, j);
std::cout << i << std::endl;
std::cout << j << std::endl;
auto k = 3.14, m = 2.7;
swap_by_ref<double>(k, m);
return 0;
}