forked from chung-kai-eng/ML-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTL_method.cpp
146 lines (129 loc) · 3.5 KB
/
STL_method.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
// the method to use STL (vector, queue, stack, set, map)
//https://larry850806.github.io/2016/06/06/STL1/
// method of vector extended function
// https://mropengate.blogspot.com/2015/07/cc-vector-stl.html
using namespace std;
class input{
public:
input(string cmd, string src, string dst, string ins_itm, string del_itm, int method){
_cmd = cmd;
_src = src;
_dst = dst;
_ins_itm = ins_itm;
_del_itm = del_itm;
_method = method;
}
void print_rlt();
private:
string _cmd;
string _src;
string _dst;
string _ins_itm;
string _del_itm;
int _method;
};
void input::print_rlt(){
cout << _cmd << ' ' << _src <<endl;
cout << _ins_itm << ' ' << _del_itm << endl;
cout << _method << endl;
}
class Rectangle{
public:
// initialization same as using _height = height;
Rectangle(int height, int weight) : _height(height), _weight(weight) {}
int area(){
return _height*_weight;
}
private:
int _xLow, _yLow, _height, _weight;
//(xLow, yLow) is the position of the left corner of rectangle
};
void vec_method();
void queue_method();
void stack_method();
void set_method();
void map_method();
int main(){
/*
Rectangle a(2,3);
cout << a.area()<<endl;
input item("INSERT", "start", "terminal", "a", "b", 2);
item.print_rlt();
*/
//vec_method();
//queue_method();
//stack_method();
//set_method();
//map_method();
return 0;
}
void queue_method(){
queue<string> q;
cout << "queue" << endl;
cout << "Queue is not an array." << endl;
cout << "Thus, we can't use index like vector." << endl;
cout << "Instead, we need to use pop to gain the element of queue." << endl;
q.push("Andy");
q.push("Cheryl");
q.push("loves");
q.push("Eric");
q.pop();
cout << "print the first element of queue : " << q.front() << endl;
cout << "print the last element of queue : " << q.back() << endl;
while(q.size() != 0){
cout << q.front() << ' ';
q.pop();
}
}
void vec_method(){
vector<int> vec;
for(int i = 0; i < 5; i++){
vec.push_back(i*10); //[0, 10, 20 ,30 ,40]
}
vec.pop_back();
vec.pop_back();
cout << "vector" << endl;
for(int i = 0 ; i < vec.size(); i++){
cout << vec[i] << ' ';
}
}
void stack_method(){
stack<string> s;
s.push("Cheryl");
s.push("loves");
s.push("Eric");
for(int i = s.size(); i != 0; i--){
cout << s.top() << ' ';
s.pop();
}
}
void set_method(){
set<int> mySet;
mySet.insert(20); // mySet = {20}
mySet.insert(10); // mySet = {10, 20}
mySet.insert(30); // mySet = {10, 20, 30}
cout << mySet.count(20) << endl; // �s�b -> 1
cout << mySet.count(100) << endl; // ���s�b -> 0
cout << "after erase : " << endl;
mySet.erase(20); // mySet = {10, 30}
cout << mySet.count(20) << endl; // 0
}
void map_method(){
map<string, int> m; // �q string ������ int
// �]�w��������
m["one"] = 1; // "one" -> 1
m["two"] = 2; // "two" -> 2
m["three"] = 3; // "three" -> 3
cout << m.count("two") << endl; // 1 -> ������
cout << m.count("ten") << endl; // 0 -> �S������
cout << m["one"] << endl; // 1
cout << m["three"] << endl; // 3
cout << m["ten"] << endl; // 0 (�L������)
}