Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ STL Tutorial

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:

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.

2. Associative containers: Store key-value pairs; each element has a key and a value, and elements are organized by key.

3. Unordered containers (introduced in C++11): Hash tables, support fast lookup, insertion, and deletion.

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:

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.