English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ STL tutorial

STL is the abbreviation of 'Standard Template Library', which is translated into Chinese as 'Standard Template Library'. STL is C++ Part of the standard library, no need to install separately.

In the previous chapters, we have already learned C++ The concept of template. C++ STL (Standard Template Library) is a powerful C++ Template class, which provides general template classes and functions. These template classes and functions can implement a variety of popular and commonly used algorithms and data structures, such as vectors, linked lists, queues, and stacks.

C++ Good support for templates, STL is to use templates to implement commonly used data structures and algorithms, and to separate data structures from algorithms. For example, the underlying of vector is an ordered table (array), list The underlying is a doubly linked list, the underlying is a circular queue, the underlying is a red-black tree, and the underlying is a hash table.

C++ The core of the standard template library includes the following three components:

ComponentsDescription
Containers (Containers)Containers are used to manage collections of a certain type. C++ Provide various types of containers, such as deque, list, vector, map, etc.
Algorithms (Algorithms)Algorithms act on containers. They provide ways to perform various operations, including initializing, sorting, searching, and transforming the contents of containers.
Iterators (iterators)Iterators are used to traverse the elements of object collections. These collections can be containers or subsets of containers.

These three components all have rich predefined functions to help us handle complex tasks in a simple way.

The following program demonstrates the vector container (a C++ The standard template library, which is very similar to arrays, differs only in that vectors automatically handle their own storage needs when expanding size:

#include <iostream>
#include <vector>
using namespace std;
 
int main()
{
   // Create a vector to store int
   vector<int> vec; 
   int i;
 
   // Display the original size of vec
   cout << "vec 的原始大小 = " << vec.size() << endl;
 
   // Add 6 values into the vector
   for(i = 0; i < 6; i++}
      vec.push_back(i+1);
   }
 
   // Display the size of vec after expansion
   cout << "vec 扩展后的大小 = " << vec.size() << endl;
 
   // Access the values in the vector 5 values
   for(i = 0; i < 5; i++}
      cout << "vec[" << i << "] 的值= " << vec[i] << endl;
   }
 
   // Use the iterator iterator to access the value
   vector<int>::iterator v = vec.begin();
   while( v != vec.end()) {
      cout << "v 的值 = " << *v << endl;
      v++;
   }
 
   return 0;
}

When the above code is compiled and executed, it will produce the following results:

The original size of vec = 0
The expanded size of vec = 6
 vec [0] value = 1
 vec [1The value of ] = 2
 vec [2The value of ] = 3
 vec [3The value of ] = 4
 vec [4The value of ] = 5
 vec [5The value of ] = 6
The value of v = 1
The value of v = 2
The value of v = 3
The value of v = 4
The value of v = 5
The value of v = 6

A few points to note about the various functions used in the above example:

  • The push_back( ) member function inserts a value at the end of the vector, and if necessary, expands the size of the vector.

  • The size( ) function displays 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.