STL in C++
In this guide, we will explore the Standard Template Library (STL) in C++, which is a powerful library that provides data structures and algorithms in an organized manner. STL helps in writing efficient and maintainable code by offering a wide range of functionalities.
1. Introduction to STL​
STL in C++ is divided into three main components:
- Containers: Data structures to store objects.
- Algorithms: Procedures to process the data stored in containers.
- Iterators: Objects that point to elements in containers and allow navigation.
2. Containers​
Containers are used to store collections of data. They are categorized into three types:
2.1 Sequence Containers​
These containers store data in a linear sequence.
| Container | Description |
|---|---|
vector | Dynamic array |
deque | Double-ended queue |
list | Doubly linked list |
Example:​
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
Output:​
1 2 3 4 5
2.2 Associative Containers​
These containers store data in key-value pairs, providing fast search and retrieval.
| Container | Description |
|---|---|
set | Collection of unique elements |
map | Key-value pairs |
multiset | Allows duplicate values |
multimap | Allows duplicate keys |
Example:​
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> students;
students[1] = "Alice";
students[2] = "Bob";
for (auto& student : students) {
cout << student.first << ": " << student.second << endl;
}
return 0;
}
Output:​
1: Alice
2: Bob
2.3 Container Adaptors​
These are wrappers around sequence containers and provide restricted interfaces.
| Container | Description |
|---|---|
stack | LIFO (Last In First Out) |
queue | FIFO (First In First Out) |
priority_queue | Elements sorted by priority |
Example (Stack):​
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> s;
s.push(10);
s.push(20);
s.push(30);
while (!s.empty()) {
cout << s.top() << " ";
s.pop();
}
return 0;
}
Output:​
30 20 10
3. Iterators​
Iterators are used to point to elements in containers. They provide a mechanism to traverse through elements.
Types of Iterators:​
- Input Iterator: Reads values in a sequence.
- Output Iterator: Writes values in a sequence.
- Forward Iterator: Can move in one direction (forward).
- Bidirectional Iterator: Can move both forward and backward.
- Random Access Iterator: Provides direct access to any element.
Example:​
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec = {10, 20, 30, 40};
vector<int>::iterator it;
for (it = vec.begin(); it != vec.end(); ++it) {
cout << *it << " ";
}
return 0;
}
Output:​
10 20 30 40
4. Algorithms​
STL provides a rich set of algorithms to work with data stored in containers. These algorithms work with iterators.
4.1 sort()​
Sorts elements in a range.
Example:​
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> vec = {4, 2, 5, 1, 3};
sort(vec.begin(), vec.end());
for (int num : vec) {
cout << num << " ";
}
return 0;
}
Output:​
1 2 3 4 5
4.2 find()​
Searches for a specific value in a range.
Example:​
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> vec = {10, 20, 30, 40, 50};
auto it = find(vec.begin(), vec.end(), 30);
if (it != vec.end()) {
cout << "Element found: " << *it << endl;
} else {
cout << "Element not found" << endl;
}
return 0;
}
Output:​
Element found: 30
4.3 reverse()​
Reverses the order of elements in a range.
Example:​
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
reverse(vec.begin(), vec.end());
for (int num : vec) {
cout << num << " ";
}
return 0;
}
Output:​
5 4 3 2 1
5. FUNCTORS :​
Functors are objects that can be treated as though they are a function. Functors are most commonly used along with STL algorithms. It overloads the function-call operator () and allows us to use an object like a function.
Types of FUNCTORS :​
- Arithmetic Functors
- Relational Functors
- Logical Functors
- Bitwise Functors
5.1 Arithmetic Functors :​
plus– Returns the sum of two parameters.minus– Returns the difference of two parameters.multiplies– Returns the product of two parameters.divides– Returns the result after dividing two parameters.modulus– Returns the remainder after dividing two parameters.negate– Returns the negated value of a parameter.
5.2 Relational Functors :​
equal_to– Returns true if the two parameters are equal.not_equal_to– Returns true if the two parameters are not equal.greater– Returns true if the first parameter is greater than the second.greater_equal– Returns true if the first parameter is greater than or equal to the second.less– Returns true if the first parameter is less than the second.less_equal– Returns true if the first parameter is less than or equal to the second.
5.3 Logical Functors :​
logical_and– Returns the result of Logical AND operation of two parameters.logical_or– Returns the result of Logical OR operation of two parameters.logical_not– Returns the result of Logical NOT operation of the parameters.
5.4 Bitwise Functors :​
bit_and– Returns the result of Bitwise AND operation of two parameters.bit_or– Returns the result of Bitwise OR operation of two parameters.bit_xor– Returns the result of Bitwise XOR operation of two parameters.
6. Commonly Used STL Algorithms​
Here are some commonly used algorithms in STL:
count(): Counts occurrences of an element.max_element(): Finds the largest element.min_element(): Finds the smallest element.accumulate(): Sums up all elements.binary_search(): Searches for an element using binary search.
Conclusion​
The STL in C++ provides a wide range of containers, iterators, and algorithms that help make your code more efficient and easier to maintain. By mastering STL, you can write cleaner and faster code. Happy coding!