The C++ Standard Library (STL, Standard Template Library) is a set of tools defined in the C++ language standard.
They provide 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 programming languages to include a core library with utilities.
This simplifies the programming process and saves us time, preventing us from reinventing the wheel. Additionally, its implementation is highly optimized.
The Standard Library consists of several key components:
Category | Description |
---|---|
Input and Output | Mechanisms for reading and writing data |
Containers | Data structures such as std::vector , std::list , and std::map . |
Algorithms | Functions such as std::sort , std::find , and std::accumulate . |
Utilities | Tools such as 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 prefix 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 naming conflicts if your project or any 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 due to the introduction of unnecessary names into the global namespace.
A middle-ground 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 in small or well-defined files, as it maintains a good balance between clarity and convenience.
It is recommended to use the explicit std:: option (option 1), occasionally combined with the partial using (option 3)
STD Functionalities
Input/Output (I/O) Functionalities
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 logging 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 quick insertions and deletions anywhere in the list.std::deque
: A double-ended 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_set
andstd::unordered_map
: Hash table-based implementations that offer average constant-time access for search, insertion, and deletion operations.
std::stack
: A data structure of type stack that follows the LIFO (Last In, First Out) principle.std::queue
: A data structure of type queue that follows the FIFO (First In, First Out) principle.std::priority_queue
: A priority queue that stores elements in such a way 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 (designed to be efficient and reusable).
std::sort
: Sorts the elements in a defined range. Uses an efficient sorting algorithm based on Quicksort.std::stable_sort
: Sorts the elements while maintaining the relative order of equivalent elements.
std::find
: Searches for an element in a range. Returns an iterator to the first found element or 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
: Evaluates whether all, any, or none of the elements in a range satisfy a condition.
Utilities in the Standard Library
The Standard Library also provides several generic classes and utilities that assist in everyday programming.
std::string
: A class that facilitates the handling of text strings, providing a series of methods to efficiently manipulate and query strings.
std::pair
: A data structure that stores two values of possibly different types. 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. 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 manipulation and access to data in containers.
Iterator Type | Description |
---|---|
Input Iterators | Allow reading elements in a container. |
Output Iterators | Allow writing elements in 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 issues such as files not found.