Vector of C++ tutorial

background

A vector is a series of objects, all of which are of the same type. Each object has an index through which the object can be retrieved. A vector is usually considered a container because it contains other objects. We will introduce the container in detail later. In order to use vector, we need to introduce relevant header files. We can use vetcor directly through the following declaration.

# include<vector>
using std::vector;

Vector is a class template. C++ has both class templates and function templates. Writing a template requires a deep understanding of C++. Later, there will be content that introduces the relevant content of C++ templates in detail. Fortunately, we don’t need to know templates well. can use it. Templates are not classes or functions themselves. On the contrary, templates are considered to be guidelines for compilers to generate classes and functions. The process by which compilers create classes and functions from templates is called instantiation. When we use templates, we need to specify the requirements to the compiler. Which type of class or function is generated, for vector, the additional information we need to provide is the type of object stored in vector.

 vector<int> ivec; //vector包含int类型
 vector<vector<string>> file; //vector存储的事vector类型

A vector is a template rather than a type, and a type derived from a vector must contain the type of the elements. We can define a vector containing any type, but we cannot define a vector of references because references are not objects.

It should be noted that the definition of vector in some old versions of C++ is different from the current one, and a space needs to be added inside the angle brackets, as shown below, and this old declaration method is required in the compiler.

vector<vector<int> > vecs;

Definition and initialization

The following provides six ways of defining and initializing vector

 vector<string> v1; // vector包含string类型对象,默认初始化,v1为空
 vector<string> v2(v1); //v2复制v1的所有内容
 vector<string> v3 = v1; //v3也是复制v1的所有内容
 vector<string> v4(4, "123"); // v4中有四个”123“
 vector<string> v5(10); //v5有10个初始化值的对象
 vector<string> v6{"12", "23"}; // 列表初始化,v6中有两个对象”12“, ”23“
 vector<string> v7 = {"21", "32"}; //列表初始化,v7中有两个对象”21“, "32"

It should be noted that the list initialization method is not supported in C++99, it must be C++11 or above, otherwise it will compile and report an error.

add element

It is better to directly initialize all elements of a vector when there is less data, or when you want to copy other vectors, or when you want to initialize all elements to a value. The more common situation is that when we create a vector, we do not Knowing how many elements are needed, or we don't know the values ​​of all the elements we need, even if we know the values ​​of all the elements, it is very tedious to specify each value when creating the vector.

A simple example, when we need a vector whose values ​​are from 0 to 9, we can easily use list initialization to initialize a vector, but what if we need a vector from 0 to 99 or 0 to 999? One of the better ways is to create an empty vector first, and then add elements at runtime through push_back. The push_back operation can place the value at the end of the vector. Examples are as follows:

vector<int> v2;
    for (int i = 0; i < 100; i++) {
        v2.push_back(i);
    }
}

In addition to push_back, vector also provides other operations, most of which are similar to the string type, as follows:

operate explain
v.empty() Determine whether the vector is empty
v.size() Get the capacity of vector
v[n] Get the nth element of the vetor
v1=v2 Copy the value of v2 to v1
v1 = {a, b, c} Replace the value of v1 with {a, b, c}

Similar to the string type, we can get the elements of the vector through the subscript, and the subscript of the vector also starts from 0. If the vector is not const, we can also get the element through the subscript and then modify its value. At the same time we can calculate a subscript and get the value at that position directly. For example, we have some scores, whose value is between 0-100, we want to divide these scores into intervals, such as 0-8, 10-19, etc., and want to calculate the number of scores in each interval.

vector<int> scores(11, 0);
    unsigned grade;
    while (std::cin >> grade) {
        ++scores[grade/10];
    }

It should be noted that the subscript can only get existing elements but cannot add elements, so it is impossible to add elements to it through the subscript of an empty vector.

Guess you like

Origin blog.csdn.net/QStack/article/details/128962208