-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.cxx
More file actions
94 lines (78 loc) · 1.69 KB
/
strings.cxx
File metadata and controls
94 lines (78 loc) · 1.69 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
92
93
94
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void functionP(int a){
cout << "pointer function: " << a <<endl;
}
void printValues(int value){
cout << value << endl;
}
void ForEach(const vector<int>& values, void(*func)(int)){
for(int value : values)
func(value);
}
int main(int argc, char *argv[])
{
string hello = "hello steven";
// cout << hello.substr(1, 3);
//backwards
int i = hello.length(); //5
string rev;
int ac = 0; //array counter
do{
rev +=hello.substr(i, 1);
i--;
} while (i != -1);
cout << rev << endl;
cout << "loop gone";
//pointers
int* pt1 = &i;
int b = 9;
*pt1 = 7;
cout << endl << i;
cout << endl << &i << endl << *pt1 << endl;
*pt1 = b;
cout << i << endl;
cout << "pointers with function" << endl;
//pointers with functions
//can use & symbol or not
auto pttFun = functionP;
pttFun(5);
void(*pt2Fun)(int);
pt2Fun = functionP;
pt2Fun(7);
typedef void(*pt3Fun)(int) ;
pt3Fun varFun = &functionP;
varFun(8);
//why we would use function pointers
vector<int> myNumbers { 2, 5, 9};
ForEach(myNumbers, printValues);
//lambda w/ function pointer in ForEach. no need for PrintValues function
ForEach(myNumbers, [] (int value){
cout << value << endl;
});
//more lamdas
auto lamda = [] (int value){
cout << value << endl;
};
ForEach(myNumbers, lamda);
cout << "lamda 1 & 2\n";
//pass a byval and b byref
int a = 7;
b = 3;
cout << a << endl << b << endl;
auto lamda1 = [a, &b] (){
// can not re-assign a
b++;
cout << a << endl << b << endl;
};
lamda1();
auto lambda2 = [a, &b] () mutable {
a++;
b--;
cout << a << endl << b << endl;
};
lambda2();
cout << a << endl << b << endl;
}