9. STL Standard Template Library (Part 1)

STL (Standard Template Library, Standard Template Library) is a general term for a series of software developed by HP Labs. Now mostly in C++, but the technology has been around for a long time before being introduced to C++.

Broadly speaking, STL is divided into three categories: algorithm (algorithm) , container (container) and iterator (iterator) , container and algorithm can be seamlessly connected through iterator. Almost all code uses template classes and template functions, which provides better code reuse opportunities than traditional libraries composed of functions and classes. In the C++ standard, the STL is organized into the following 13 header files: <algorithm>, <deque>, <functional>, <iterator>, <vector>, <list>, <map>, <memory>, <numeric> , <queue>, <set>, <stack>, and <utility>.

 

container

The number of classic data structures is limited, but we often repeat some codes written to implement structures such as vectors and linked lists. These codes are very similar, but differ in details in order to adapt to changes in different data. The STL container provides us with such convenience. It allows us to reuse the existing implementation to construct our own data structure of a specific type. By setting some templates, the STL container provides support for the most commonly used data structures. Parameters allow us to specify the data type of the elements in the container, simplifying a lot of our repetitive and tedious work.

The container part mainly consists of header files <vector>, <list>, <deque>, <set>, <map>, <stack> and <queue>.

 

iterator

Iterators are used in the STL to link algorithms to containers and act as a glue. Almost all algorithms provided by STL work through iterators to access element sequences. Each container defines its own exclusive iterator to access the elements in the container.

The iterator part mainly consists of header files <utility>, <iterator> and <memory>.

 

algorithm

STL provides about 100 template functions for implementing algorithms, such as the algorithm for_each will call the specified function for each element in the specified sequence, stable_sort will sort the sequence stability according to the rules you specify, and so on. In this way, as long as you are familiar with STL, many codes can be greatly simplified, and you only need to call one or two algorithm templates to complete the required functions and greatly improve the efficiency.
    The algorithm part mainly consists of header files <algorithm>, <numeric> and <functional>. <algorithm> is the largest of all STL header files (although it is well understood), it is composed of a large number of template functions, each function can be considered to be largely independent, among which the commonly used The range of functions involves compare, swap, find, traverse operations, copy, modify, remove, reverse, sort, merge, and more. <numeric> is small in size and only includes a few template functions that perform simple mathematical operations on sequences, including addition and multiplication on sequences. <functional> defines some template classes to declare function objects.


 

Iterator Fundamentals

  • An iterator is an object that "traverses all or some of the elements in an STL container".
  • An iterator points to a specific location in the container.
  • An iterator is like a pointer.
  • Iterators provide access to objects in a container, and can define the scope of objects in the container.

Input iterator : It is also called "read-only iterator". It reads elements from the container. It can only read one element at a time and move forward. It only supports one algorithm. The same input iterator cannot be traversed twice. a sequence.
Output iterator : It is also called "write-only iterator". It writes elements into the container and can only write one element at a time to move forward. It only supports one algorithm. The same output iterator cannot be traversed twice. a sequence.
Forward iterator : Combines the functions of input iterator and output iterator, and can also parse a position specified by an iterator multiple times, and can read/write a value multiple times.
Bidirectional Iterator : Combines the functionality of forward iterators, but also moves backwards by the -- operator.
Random access iterator : Combine the functions of bidirectional iterators, you can also skip forward and backward any number of positions, and you can directly access elements at any position in the container

 

Bidirectional Iterators vs Random Access Iterators

Operations supported by bidirectional iterators:
it++, ++it, it--, --it, *it, itA = itB,
itA == itB, itA != itB
          where list, set, multiset, map, and multimap support bidirectional iteration device.
Operations supported by random access iterators:
add 
it+=i, it-=i, it+i (or it=it+i), it[i],
itA<itB, itA<= to the operations of bidirectional iterators itB, itA>itB, itA>=itB function.
          Among them, vector and deque support random access iterators.

 

 

Container introduction

string

string is the string type of STL, usually used to represent strings. Before using string, strings were usually represented by char*. Both string and char* can be used to represent strings, so what is the difference between the two?
    Comparison of string and char*

  • string is a class and char* is a pointer to a character.
  • String encapsulates char*, manages this string, and is a container of char* type.
  • string does not consider memory release and out-of-bounds.
  • string manages the memory allocated by char*. Each time the string is copied, the value is maintained by the string class, so there is no need to worry about copying out of bounds and out-of-bounds values.
  • string provides a series of string manipulation functions - find find, copy copy, delete erase, replace replace, insert insert

         

vector

  • A vector is a container that manages elements in a dynamic array.
  • A vector can access elements randomly (supports direct access by index value, using the [] operator or the at() method).
  • Adding or removing elements from the tail of a vector is very fast. But inserting or removing elements in the middle or at the head is time consuming

 

about

  • Deque is the abbreviation of "double-ended queue". Like vector, it is a container of STL. Deque is a double-ended array, and vector is single-ended.
  • Deque is very similar to vector in interface and can be directly replaced in many operations.
  • The deque can access elements randomly (supports direct access to index values, using the [] operator or the at() method, which will be discussed in detail later).
  • Adding or removing elements from the deque head and tail is very fast. But inserting or removing elements in the middle is time-consuming.

 

stack 

  • stack is a stack container, which is a kind of "first in, last out" container.
  • A stack simply decorates the deque container to become another type of container.

 

queue

  • Queue is a queue container, which is a "first in, first out" container.
  • A queue simply decorates a deque container to become another type of container.

 

list

  • list is a doubly linked list container that can efficiently insert and delete elements.
  • Lists cannot randomly access elements, so the at.(pos) function and the [] operator are not supported. It++(ok) it+5(err)

 

set/multiset

  • A set is a collection container, the elements contained in it are unique, and the elements in the collection are arranged in a certain order. The element insertion process is inserted according to the sorting rules, so the insertion position cannot be specified.
  • Set is implemented using the data structure of a red-black tree variant, which is a balanced binary tree. Faster than vector for insertion and deletion.
  • A set cannot directly access elements. (You cannot use the at.(pos) and [] operators).
  • The difference between multiset and set: set supports unique key values, and each element value can only appear once; while the same value in multiset can appear multiple times.
  • You cannot directly modify the values ​​of elements in a set or multiset container, because such containers are automatically sorted. If you want to modify the value of an element, you must first delete the original element, and then insert a new element.

 

map/multimap

  • A map is a standard associative container, and a map is a sequence of key-value pairs, ie (key, value) pairs. It provides fast key-based retrieval capabilities.
  • The key value in the map is unique. The elements in the collection are arranged in a certain order. The element insertion process is inserted according to the sorting rules, so the insertion position cannot be specified.
  • The specific implementation of map adopts the data structure of the balanced binary tree of the red-black tree variant. Faster than vector for insertion and deletion.
  • The map can directly access the value corresponding to the key, and supports the [] operator, such as map[key]=value.
  • The difference between multimap and map: map supports unique key values, and each key can only appear once; while the same key in multimap can appear multiple times. multimap does not support the [] operator


Common Mechanisms of Containers

All containers provide value semantics, not reference semantics. When the container performs the operation of inserting an element, the copy action is implemented internally. So the elements stored in the STL container must be able to be copied (must provide a copy constructor).

  • Except for queue and stack, each container provides functions that return iterators, and elements can be accessed using the returned iterators.
  • Usually STL doesn't throw exceptions. Ask the user to ensure that the correct parameters are passed in.
  • Each container provides a default constructor and a default copy constructor.

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324357106&siteId=291194637