The Standard Template Library (STL) is a powerful set of C++ template classes to implement generic data structures and algorithms. STL provides a collection of template classes and functions, which allows programmers to implement complex data structures and algorithms without the need to create them from scratch.
The main components of the STL include:
Containers are used to store and organize data. They can be divided into two categories:
Example:
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// Access elements using index
std::cout << "Element at index 2: " << vec[2] << std::endl;
// Add element to the end
vec.push_back(6);
std::cout << "Last element: " << vec.back() << std::endl;
return 0;
}
Example:
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> m;
m[1] = "one";
m[2] = "two";
m[3] = "three";
for (auto& pair : m) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
The STL provides a variety of algorithms to manipulate data in containers. These algorithms work seamlessly with any container that provides iterators.
Some of the common STL algorithms include:
std::sort(), std::stable_sort()std::find(), std::binary_search()std::reverse(), std::transform()std::equal(), std::lexicographical_compare()std::count(), std::count_if()Example:
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> vec = {5, 3, 8, 1, 4};
// Sorting vector
std::sort(vec.begin(), vec.end());
// Printing sorted vector
for (int num : vec) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
Iterators are objects that allow traversal through the elements of a container. They provide a way to access elements of a container in a generalized manner, making it easy to write algorithms that work with different types of containers.
There are several types of iterators:
std::istream_iterator).std::ostream_iterator).Example of using iterators to traverse a vector:
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// Using an iterator to traverse and print elements
for (auto it = vec.begin(); it != vec.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
A function object (or functor) is an object that can be used as if it were a function. It is any object that overloads the operator().
Function objects are useful when you need a custom function to be passed as a parameter to algorithms or containers, especially when you want to capture additional state.
Example:
#include <iostream>
#include <vector>
#include <algorithm>
class Print {
public:
void operator()(int value) const {
std::cout << value << " ";
}
};
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// Using a function object with an algorithm
std::for_each(vec.begin(), vec.end(), Print());
std::cout << std::endl;
return 0;
}
Allocators are responsible for handling memory allocation and deallocation for STL containers. Most STL containers (such as std::vector or std::list) use the default allocator, but custom allocators can be implemented if needed to control memory management more precisely.
The Standard Template Library (STL) is an essential part of C++ that provides a set of powerful, flexible, and reusable templates for common data structures and algorithms. With containers, algorithms, iterators, and other components, STL helps developers write efficient, generic, and maintainable code. Whether you're implementing a custom data structure or using a pre-built algorithm, STL offers significant advantages for both performance and productivity.
Open this section to load past papers