C++ STL Tutorial

0. Foreword

C++ Standard Library headers

Containers, Algorithms, Iterators

1. Containers

1.1 Vector

Example 1 - Basic Operation:

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
#include <iostream>
#include <vector>

using namespace std;

void output(string op, vector<int>& v) {
cout << "After " + op << endl;
for (auto i : v) {
cout << i << " ";
}
cout << endl;
}

int main() {
vector<int> v;

// Add
v.push_back(9);
v.push_back(8);
v.push_back(5);
output("Add", v);

// Delete
v.erase(v.begin());
output("Delete", v);

// Update
v[0] = 6;
output("Update", v);

return 0;
}

Example 2 - Sort:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
vector<int> v = { 7, 1, 3, 4, 9 };
sort(v.begin(), v.end());

for (auto i : v) {
cout << i << " ";
}

return 0;
}

1.2 Queue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <queue>

using namespace std;

int main() {
queue<int> q;
q.push(9);
q.push(8);
q.push(5);

cout << "q.front()\t" << q.front() << endl;
cout << "q.back()\t" << q.back() << endl;

q.pop();
cout << "After q.pop()" << endl;
cout << "q.front()\t" << q.front() << endl;

return 0;
}

1.3 Stack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <stack>

using namespace std;

int main() {
stack<int> s;
s.push(9);
s.push(8);
s.push(5);

cout << "s.top()\t\t" << s.top() << endl;

s.pop();
cout << "After s.pop()" << endl;
cout << "s.top()\t\t" << s.top() << endl;

return 0;
}

1.4 Set

Example 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <set>

using namespace std;

int main() {
set<int> s;

s.insert(2);
s.insert(1);
s.insert(1);
s.insert(3);

for (auto i : s) {
cout << i << " ";
}

return 0;
}

Example 2:

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
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

int main() {
vector<int> v;

set<int> s1 = { 1, 2, 3, 5, 7 };
set<int> s2 = { 1, 3, 5, 7, 9 };

// Union
cout << "set_union()" << endl;
set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), back_inserter(v));
for (auto i : v) {
cout << i << " ";
}

// Intersection
v = {};
cout << endl <<"set_intersection()" << endl;
set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), back_inserter(v));
for (auto i : v) {
cout << i << " ";
}

return 0;
}

1.5 Map

1.5.1 map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <map>

using namespace std;

int main() {
map<int, string> m;

m[2] = "小明";
m[1] = "小红";
m[3] = "小强";


for (const auto& p : m) {
cout << p.first << " " << p.second << endl;
}

return 0;
}

1.5.2 unordered_map

2. Algorithms

sort

includes

set_union

set_intersection

set_difference

binary_search

3. Iterators


C++ STL Tutorial
https://www.hardyhu.cn/2023/01/28/C-STL-Tutorial/
Author
John Doe
Posted on
January 29, 2023
Licensed under