The standard library of C++ (STL, Standard Template Library) is a set of tools defined in the C++ language standard.
It provides a set of predefined classes and functions that address common tasks, such as data management, collection manipulation, and the use of generic algorithms.
It is very common for languages to include a main library with utilities.
This simplifies the programming process and allows us to save time, avoiding the need to reinvent the wheel. Furthermore, its implementation is highly optimized.
The Standard Library is composed of several key components:
| Category | Description |
|---|---|
| Input and Output | Mechanisms for reading and writing data |
| Containers | Data structures like std::vector, std::list, and std::map. |
| Algorithms | Functions like std::sort, std::find, and std::accumulate. |
| Utilities | Tools like std::pair, std::tuple. |
| Iterators | Objects that allow traversing and manipulating the elements of containers. |
How to Use STD
When working with STL elements, there are several ways to handle the std namespace (where all these tools are defined).
The most explicit and safe way is to always prepend std:: to each element.
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3};
std::cout << "Example with explicit std:::" << std::endl;
for (std::size_t i = 0; i < numbers.size(); ++i) {
std::cout << numbers[i] << " ";
}
return 0;
}
This avoids name conflicts if your project or an external library defines something similar.
Another option is to use the using namespace std; statement, which eliminates the need to write std:: repeatedly.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3};
cout << "Example with using namespace std:" << endl;
for (size_t i = 0; i < numbers.size(); ++i) {
cout << numbers[i] << " ";
}
return 0;
}
But it can cause problems in large projects by introducing unnecessary names into the global namespace.
An intermediate alternative is to specifically declare the tools you use.
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
int main() {
vector<int> numbers = {1, 2, 3};
cout << "Example with specific using:" << endl;
for (size_t i = 0; i < numbers.size(); ++i) {
cout << numbers[i] << " ";
}
return 0;
}
This last option is preferable if you work on small or well-delimited files, as it maintains a good balance between clarity and convenience.
It is recommended to use the explicit std:: option (option 1), combined occasionally with the partial using option (option 3).
Functionalities of the STD
The C++ standard library offers a set of tools for data input and output.
std::cin: Object for standard input (keyboard).std::cout: Object for standard output (screen).std::cerr: Object for error output.std::clog: Object for log message output.
std::ifstream: For reading data from files.std::ofstream: For writing data to files.std::fstream: For reading and writing to files.
Containers in the Standard Library
Containers are classes that store and manage collections of data. There are different types of containers, each optimized for different usage scenarios.
std::vector: A container that stores elements in a dynamic array. Allows fast access to elements and dynamic resizing.std::list: A container that implements a doubly linked list, ideal for fast insertions and deletions anywhere in the list.std::deque: A double-ended queue container that allows efficient access at both ends of the container.
std::set: Stores unique elements in a self-balancing binary tree. Allows fast searches.std::map: Stores unique key-value pairs. Similar tostd::set, but with key-value association.std::unordered_setandstd::unordered_map: Hash table-based implementations that offer constant average-time access for search, insertion, and deletion operations.
std::stack: A stack data structure that follows the LIFO (Last In, First Out) principle.std::queue: A queue data structure that follows the FIFO (First In, First Out) principle.std::priority_queue: A priority queue that stores elements so that the element with the highest priority is extracted first.
Algorithms in the Standard Library
The Standard Library includes a wide range of algorithms that work with containers (they are designed to be efficient and reusable).
std::sort: Sorts elements in a defined range. Uses an efficient sorting algorithm based on Quicksort.std::stable_sort: Sorts elements while maintaining the relative order of equivalent elements.
std::find: Searches for an element in a range. Returns an iterator to the first element found or to the end of the range if not found.std::binary_search: Searches for an element in a sorted range.
std::transform: Applies a function to each element in a range and stores the result in another range.std::accumulate: Accumulates values in a range using a binary operation.
std::count: Counts the number of occurrences of a value in a range.std::all_of,std::any_of,std::none_of: Evaluate whether all, any, or none of the elements in a range meet a condition.
Utilities in the Standard Library
The standard library also provides several generic classes and utilities that aid in daily programming.
std::string: A class that facilitates handling text strings, providing a series of methods to manipulate and query strings efficiently.
std::pair: A data structure that stores two values of possibly different types. It is frequently used to return multiple values from a function.std::tuple: Similar tostd::pair, but allows storing a variable number of elements of different types.
std::optional: Represents a value that may or may not be present. It is used to indicate the absence of a value without using pointers.
std::variant: Allows storing one of several data types. It is a safe alternative to type pointers and is used to represent values that can be of different types.
Iterators in the Standard Library
Iterators are objects that allow traversing the elements of containers. They act like pointers and allow manipulating and accessing data in containers.
| Iterator Type | Description |
|---|---|
| Input Iterators | Allow reading elements from a container. |
| Output Iterators | Allow writing elements to a container. |
| Bidirectional Iterators | Allow moving forward and backward in a container. |
| Random Access Iterators | Allow random access to elements. |
Exceptions and Error Handlers
The Standard Library also includes mechanisms for error handling through exceptions.
For example, when working with files, the library provides specific exceptions to handle problems like files not found.
