-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstl_topic.readme
More file actions
143 lines (110 loc) · 4.46 KB
/
stl_topic.readme
File metadata and controls
143 lines (110 loc) · 4.46 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# C++ Standard Template Library (STL) - Complete Overview
## What is STL?
The **Standard Template Library (STL)** is a powerful library in C++ that provides ready-to-use classes and functions for common data structures and algorithms.
It saves time by providing generic containers, iterators, and algorithms which can be reused for different data types.
---
## Main Components of STL
1. **Containers** — Data structures to store objects
2. **Algorithms** — Functions that operate on containers
3. **Iterators** — Objects that point to container elements and allow traversal
4. **Function Objects (Functors)** — Objects acting like functions
5. **Adapters** — Containers with restricted interfaces (stack, queue, etc.)
---
## 1. Containers
Containers store collections of objects. They can be:
| Container | Description | Header | Example Declaration |
|-------------------|---------------------------------------|---------------|-------------------------------|
| `vector` | Dynamic array (resizable) | `<vector>` | `vector<int> v;` |
| `list` | Doubly linked list | `<list>` | `list<int> l;` |
| `deque` | Double-ended queue (fast front/back) | `<deque>` | `deque<int> d;` |
| `set` | Sorted unique elements | `<set>` | `set<int> s;` |
| `map` | Key-value pairs (sorted by key) | `<map>` | `map<string, int> m;` |
| `unordered_map` | Key-value pairs, no order | `<unordered_map>` | `unordered_map<int, string> um;` |
### Example: vector
```cpp
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
v.push_back(4); // Add element at end
cout << v[0] << endl; // Access element by index
cout << v.size() << endl; // Get size of vector
}
Example: sort and find
cpp
Copy
Edit
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {4, 2, 5, 1, 3};
sort(v.begin(), v.end()); // Sort the vector
for (int x : v) cout << x << " "; // Print sorted vector
cout << endl;
// Find element 3
auto it = find(v.begin(), v.end(), 3);
if (it != v.end()) cout << "Found 3 at index " << (it - v.begin()) << endl;
}
3. Iterators
Iterators are pointers or objects that traverse containers. They allow algorithms to work on different containers uniformly.
Iterator Description
begin() Points to first element
end() Points just past the last element
rbegin() Reverse iterator at last element
rend() Reverse iterator before first element
Example: Using iterators
cpp
Copy
Edit
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {10, 20, 30};
for (auto it = v.begin(); it != v.end(); ++it) {
cout << *it << " "; // Dereference iterator to get element
}
cout << endl;
}
4. Function Objects (Functors)
Function objects are objects that behave like functions by overloading the operator(). Used for custom behavior in algorithms.
5. Adapters
Adapters provide restricted interfaces on top of other containers:
Adapter Description Header
stack Last-in-first-out (LIFO) <stack>
queue First-in-first-out (FIFO) <queue>
priority_queue Elements with priority <queue>
Example: stack
cpp
Copy
Edit
#include <stack>
#include <iostream>
using namespace std;
int main() {
stack<int> s;
s.push(1);
s.push(2);
cout << s.top() << endl; // Output: 2
s.pop();
cout << s.top() << endl; // Output: 1
}
Summary Table
Component Purpose Common Headers
Containers Store data <vector>, <list>, <map>, <set>
Algorithms Perform operations <algorithm>
Iterators Traverse containers Provided by container headers
Function Objects Custom callable objects <functional>
Adapters Restricted interface containers <stack>, <queue>
Additional Tips
STL makes code cleaner, faster, and easier to maintain.
Use auto keyword with iterators for simplicity.
Algorithms use iterators, so they work with any container supporting those iterators.
Learn containers and algorithms gradually with practice.
References & Further Reading
cplusplus.com - STL
cppreference.com - STL
GeeksforGeeks - STL Tutorial