[C++] STL Standard Template Library (Examples of common operations of Iterator iterators)

❥  Mainly used for self-review, plus experience exchange and sharing, updated every  day❥

background

A library is a collection of program components that can be reused in different programs. ANSI C++ includes a C++ STL (Standard Template Library), the C++ Standard Template Library (C++ Generic Library), which defines commonly used data structures and algorithms in the std namespace, which is very convenient to use.

STL code is broadly divided into three categories: algorithms, containers, and iterators. We've been using the standard library heavily, all math functions, string manipulation functions, and input/output implementations using well-proven, reusable algorithms.

After you are familiar with STL, you will find that many "complex and interesting functions" code that you have written before can now be replaced with just a few lines. By calling the algorithm template, you can get some efficient code to better complete the required functions.

❀The following mainly introduces various commonly used containers, iterators and algorithms in C++ STL.


iterator iterator

1 Introduction

Iterators act as glue in the STL, holding together the various parts of the STL. It is also a data type that inspects the elements inside the container and iterates over the elements. C++ tends to use iterators rather than subscripting operations , because the  standard library defines an iterator type for each standard container (such as vector), and only a few containers (such as vector) support subscripting operations to access container elements .

2. Definition and initialization

Each container defines its own iterator type, such as vector:

vector<int>::iterator it; //定义一个名为 it 的变量

Each container defines a pair of functions named begin and end that return an iterator.

The iterator is initialized as follows:

vector<int> ivec;
vector<int>::iterator it1 = ivec.begin(); //将迭代器it1指向ivec容器的第一个元素
vector<int>::iterator it2 = ivec.end();   //将迭代器it2指向ivec容器的最后一个元素的下一个位置

Note: end does not point to any element of the container, but points to the next position of the last element of the container , called an out-of-end iterator . If the vector is empty, the iterator returned by begin is the same as the iterator returned by end. Once defined and initialized as above, it is equivalent to a certain association between the iterator and the container, just like initializing a pointer to a certain space address.

3. Common operations

First declare an ivec container of vector<int>, then use an iterator to traverse the ivec container and reset each element to 0

vector<int> ivec;
vector<int>::iterator it;
for(it = ivec.begin(); it != ivec.end(); ++it)
{
    *it = ();
}

Of the container types defined in C++, only the vector and queue containers provide iterator arithmetic operations and relational operations other than != and ==:

it ± n     //在迭代器上 加上/减去 整数n,将产生指向容器中 前面/后面 第 n个元素的迭代器
           //新计算出的迭代器必须指向容器中的元素或超出容器末端的下一个元素
it1 ±= it2 //将it1 加上/减去 it2的运算结果赋给it1
           //两个迭代器必须指向容器中的元素或超出容器末端的下一个元素
it1-it2    //两个迭代器相减得出两个迭代器的距离,必须指向容器中的元素或超出容器末端的下一个元素
>,>=,<,<=  //元素靠后的迭代器大于靠前的迭代器,必须指向容器中的元素或超出容器末端的下一个元素

Guess you like

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