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 languages to include a main library with utilities.
This simplifies the programming process and allows us to save 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 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 traversal and manipulation of elements in containers. |
How to Use STD
When working with elements of the STL, 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 name conflicts if your project or an external library defines something similar.
Another option is to use the statement using namespace std;, 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 are working in small or well-defined files, as it maintains a good balance between clarity and convenience.
It is advisable 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 provides a set of tools for input and output of data.
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 messages output.
std::ifstream
: For reading data from files.std::ofstream
: For writing data to files.std::fstream
: For reading and writing 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 quick 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 container that allows efficient access at both ends of the container.
std::set
: Stores unique elements in a self-balancing binary tree. Allows for fast searches.std::map
: Stores unique key-value pairs. Similar tostd::set
, but with key-value associations.std::unordered_set
andstd::unordered_map
: Hash table-based implementations that provide average constant 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 in such a way that the highest priority element 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 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 meet 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 manipulate and query strings efficiently.
std::pair
: A data structure that stores two values of possibly different types. Often 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 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 issues like file not found.