C++ STL Tutorial
The C++ Standard Template Library (STL) is a powerful collection of C++ template classes and functions that provides a series of general-purpose, reusable algorithms and data structures.
The design of STL is based on generic programming, which means that templates can be used to write code that is independent of any specific data type.
STL is divided into multiple components, including Containers, Iterators, Algorithms, Function Objects, and Adapters.
Benefits of using STL:
- Code Reuse: STL provides a large number of general-purpose data structures and algorithms, reducing the work of repeatedly writing code.
- Performance optimization: The algorithms and data structures in STL are optimized to provide the best performance.
- Generic Programming: Using templates, STL supports generic programming, enabling algorithms and data structures to be applied to any data type.
- Easy to Maintain: The design of STL makes the code more modular and easier to read and maintain.
The core of the C++ Standard Template Library includes the following important components:
| Component | Description |
|---|---|
| Containers |
Containers are one of the most basic components of STL, providing various data structures, including vector, list, queue, stack, set, map, etc. These containers have different characteristics and uses, and the appropriate container can be selected according to actual needs. |
| Algorithms | STL provides a large number of algorithms for performing various operations on elements in containers, including sorting, searching, copying, moving, transforming, etc. These algorithms do not need to care about the specific type of container when used; you only need to specify the range to operate on. |
| Iterators | Iterators are used to traverse elements in a container, allowing elements to be accessed in a uniform way without caring about the internal implementation details of the container. STL provides various types of iterators, including random access iterators, bidirectional iterators, forward iterators, and input/output iterators. |
| Function Objects | A function object is an object that can be called like a function and can be used for various operations in algorithms. STL provides a variety of function objects, including unary function objects, binary function objects, predicates, etc., to meet different needs. |
| Adapters | Adapters are used to adapt one container or iterator into another container or iterator to meet specific needs. STL provides a variety of adapters, including stack adapters, queue adapters, and priority queue adapters. |
These components all come with rich predefined functions that help us handle complex tasks in a simple way.
Containers
Containers are sequences used to store data; they provide different storage methods and access patterns.
Containers in STL can be divided into three categories:
1. Sequence containers: Store a sequence of elements and allow bidirectional traversal.
- std::vector: Dynamic array, supports fast random access.
- std::deque: Double-ended queue, supports fast insertion and deletion.
- std::list: Linked list, supports fast insertion and deletion, but does not support random access.
2. Associative containers: Store key-value pairs; each element has a key and a value, and elements are organized by key.
- std::set: Set, does not allow duplicate elements.
- std::multiset: Multiset, allows multiple elements to have the same key.
- std::map: Map, each key maps to one value.
- std::multimap: Multimap, stores key-value pairs, where the key is unique but the value can be duplicated, allowing one key to map to multiple values.
3. Unordered containers (introduced in C++11): Hash tables, support fast lookup, insertion, and deletion.
- std::unordered_set: Unordered set.
- std::unordered_multiset: Unordered multiset.
- std::unordered_map: Unordered map.
- std::unordered_multimap: Unordered multimap.
The program below demonstrates the vector container (a C++ standard template), which is very similar to an array, with the only difference being that a vector automatically handles its own storage needs when it needs to grow in size:
Example
#include
<
iostream
#include
<
vector
using
namespace
std
;
int
main
(
)
{
// Create a vector to store int
vector
<
int
vec
;
int
i
;
// Show the original size of vec
cout
<<
"
vector size =
"
<<
vec
.
size
(
)
<<
endl
;
// Push 5 values into the vector
for
(
i
=
0
;
i
<
5
;
i
++
)
{
vec
.
push_back
(
i
)
;
}
// Show the size of vec after expansion
cout
<<
"
extended vector size =
"
<<
vec
.
size
(
)
<<
endl
;
// Access the 5 values in the vector
for
(
i
=
0
;
i
<
5
;
i
++
)
{
cout
<<
"
value of vec [
"
<<
i
<<
"
] =
"
<<
vec
[
i
]
<<
endl
;
}
// Access values using the iterator
vector
<
int
::
iterator
v
=
vec
.
begin
(
)
;
while
(
v
!=
vec
.
end
(
)
)
{
cout
<<
"
value of v =
"
<< *
v
<<
endl
;
v
++;
}
return
0
;
}
When the code above is compiled and executed, it produces the following result:
vector size = 0
extended vector size = 5
value of vec [0] = 0
value of vec [1] = 1
value of vec [2] = 2
value of vec [3] = 3
value of vec [4] = 4
value of v = 0
value of v = 1
value of v = 2
value of v = 3
value of v = 4
There are a few points to note about the various functions used in the example above:
- The push_back() member function inserts a value at the end of the vector, expanding the vector's size if necessary.
- The size() function shows the size of the vector.
- The begin() function returns an iterator pointing to the beginning of the vector.
- The end() function returns an iterator pointing to the end of the vector.
STL is an indispensable part of C++ programming; it greatly extends the functionality of C++, enabling programmers to write more efficient and more readable code.
Mastering the use of STL is very important for any C++ programmer.