-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
57 lines (45 loc) · 1.33 KB
/
Copy pathmain.cpp
File metadata and controls
57 lines (45 loc) · 1.33 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
#include <iostream>
#include <vector>
using namespace std;
// ===============================================
// avoid large copying of data
// bad - pass by value
// good (but not ideal for end users) - pass by reference
// best - use const (notify compiler + end users)
void print_v1(std::vector<int> v);
void print_v2(std::vector<int>& v);
void print_v3(const std::vector<int>& v);
// void print1(vector<int> v);
// void print2(vector<int>& v);
// void print3(const vector<int>& v);
// 🐢 vector<int> v
// 🔥 vector<int>& v
// ===============================================
// when you want to update the array
// incorrect - will make copy, original vector unchanged
// incorrect - compiler error when updating vector
// correct - pass by reference to mutate array
// void add_one_v1(std::vector<int> v);
// void add_one_v2(const std::vector<int>& v);
void add_one_v3(std::vector<int>& v);
// void normalize(Vector3& v);
// ===============================================
void add_one(int x) {
x++;
}
void add_one_by_ref(int& x) {
x++;
}
int main() {
int a = 1;
add_one(a);
std::cout << "add_one a: " << a << std::endl;
add_one_by_ref(a);
std::cout << "add_one_by_ref a: " << a << std::endl;
// std::vector<int> v{1, 2, 3};
// print_vec_v1(v);
// print_vec_v1(v);
// print_vec_v2(v);
// print_vec_v3(v);
return 0;
}