Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
122 lines (89 loc) · 2.2 KB
/
Copy pathmain.cpp
File metadata and controls
122 lines (89 loc) · 2.2 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
// Vectors
#include <vector>
void vectors_test(){
std::vector<int> v1;
std::vector<int> v2 {1, 2, 3};
std::vector<int> v3 (26);
v1.push_back(11);
std::cout << v1[0] << std::endl;
for (int i=0; i<v3.size(); i++){
std::cout << v3[i] << std::endl;
}
bool is_empty = v1.empty();
int end = v2.back();
v2.pop_back();
}
// Sets
#include <set>
void set_test(){
std::set<int> s1;
std::set<int> s2 { 1, 2, 3, 4, 5 };
std::vector<int> v {7, 8, 9};
std::set<int> s3 (v.begin(), v.end());
s1.insert(69);
s1.erase(69);
int val_is_in_set = s2.count(3);
int num_elements = s2.size();
}
// Hash Maps
#include <unordered_map>
void map_test(){
std::unordered_map<int, int> map1;
std::unordered_map<int, int> map2 {{1, 11}, {2, 22}};
if (map2.count(2)){
std::cout << map2[2] << std::endl;
} else {
map2[2]=22;
}
int size = map2.size();
for (auto [key, val] : map2){
std::cout << key << ": " << val << std::endl;
}
for (auto x: map2){
int key = x.first;
int val = x.second;
}
}
// Queues
#include <queue>
void queue_test(){
// if we want to push and pop from both sides of the queue, need to use deque (double ended queue) instead.
std::queue<int> q1;
std::deque<int> q2;
q2.push_front(1);
q2.push_back(2);
int back = q2.back();
int front = q2.front();
q2.pop_back();
q2.pop_front();
q1.push(66); // push to back of queue
q1.push(77);
int front2 = q1.front();
int back2 = q1.back();
q1.pop(); // pops from left side of queue
}
// Heaps
// priority queue is included in the queue package.
void test_heap(){
// Max heap by default
std::priority_queue<int> heap1; // cant initialize
std::vector<int> arr {1, 2, 3};
std::priority_queue<int> heap2 (arr.begin(), arr.end());
heap1.push(11);
heap1.push(22);
heap1.push(33);
int top = heap1.top();
heap1.pop();
bool is_empty = heap1.empty();
int size = heap2.size();
}
// Main Entry
int main(){
vectors_test();
queue_test();
test_heap();
// set_test();
// map_test();
return 0;
}