vector container of C + + high-order STL

The vector container is the best alternative to arrays

vector container can also be used to save and manage a large amount of data of the same type, but it is superior to array in terms of memory management and access to data elements:
The dynamic increase or decrease of the capacity of the vector container replaces the fixed capacity of the array, which can meet various needs;
Operating data elements through the function functions provided by the vector container replaces the direct operation of data elements through "[]", which is not only safer, but also provides richer operations.
Therefore, the vector container has also become the best substitute for arrays and is our most commonly used STL container.

Create and initialize the vector object

The vector container is a class template vector. Therefore, to use the vector container, we first need to instantiate a specific type of template class according to the type of data we want to save, and then create the corresponding vector container object. Finally, we can use the function functions it provides to add, delete, insert and other common operations on the container, Complete the management of data.

Empty container initialization

#Include < vector > / / import the header file of the vector class template
using namespace std; // Use the namespace std where the vector is located
// ...
// To save the Employee object, first instantiate the vector class template with the Employee type,
// Then create the instance object vecEmp
vector<Employee> vecEmp;

Initialization with default data

Specify the default data and the number of default data
If you want to get a vector container that has saved the default data, you can use its constructor to specify the default data and the number of default data when creating the container object. When the container object is created, the default data will be saved in the container.

Employee emp; // Default data
// Create 4 copies of emp objects and save them to the container
vector<Employee> vecEmp(4, emp);
// Use the default constructor of the Employee class to create four objects and save them to the container
vector<Employee> vecDefEmp(4);

Use initialization list

// Create a vector container that holds student grades
// Use the initialization list to add initial data while creating
vector<int> vecScores = { 87, 72, 63, 93 };

The reserve() function requests enough memory

If the maximum possible capacity of the vector container is known in advance, you can use the reserve() function to apply for enough memory and reserve enough element positions for the vector container to reduce the dynamic memory application process during the element addition process.

// There are about 1000 employees, so 1000 element positions are reserved for the vector container
vecEmp.reserve( 1000 );

array container

A fixed number of data of the same type shall be saved, such as the average temperature in 12 months of a year, PM 2.5 value seven days a week, etc. In this case, if the vector container is still used to store these data, the dynamic memory of the vector container will often apply for more space than the actual data, which will waste valuable memory resources; The vector container provides a push_ The back () function can dynamically add data to the container and, if unintentionally, call push_. The back() function will destroy the fixed number of this group of data. We should not only make use of the advantages of the container, but also avoid the disadvantages of the vector container in saving this kind of data. STL provides the spare wheel of the vector container - array container.

Array is a fixed size container that supports random access. Unlike the vector container, which reserves space for elements, the array container does not reserve excess space, but only allocates necessary space for elements. Therefore, it overcomes the disadvantage that the vector container wastes memory resources when saving a fixed number of data; At the same time, different from the ordinary array, it saves its own size information and provides the necessary operation functions, which can operate the data conveniently and safely, which makes it make up for the disadvantage of the ordinary array.

The array container is also a class template, but it requires two specialized parameters. The first parameter represents the data type that the container can save, and the second parameter represents the number of data that the container can save.

// Introduce the header file that defines the array container
#include <array>
using namespace std;
// Define an array container that can hold only 12 double type data elements
array<double, 12> arrMonths;
// Like using an ordinary array, use subscripts to assign values to the data elements in the container and save the data to the container
arrMonths[0] = 15.4;
arrMonths[1] = 17.6;
// ...
arrMonths[11] =19.7;
Similarly, we can use iterators to access array Data in container:
// Output all data in the array container
for(auto it = arrMonths.begin();
it != arrMonths.end(); ++it )
{
	// Using iterators to access data in the array container
	cout<<*it<<endl;
}

Except that the array container cannot change its size dynamically, the operation of the array container is not much different from that of the vector container.

Operation function of vector container


For example: push_back() function
Using push_ When the back() function adds data to the vector container, it will accept a data with the same data type as the parameter data of the vector container template, and then copy it as a new element and add it to the end of the vector container, that is, push the data to the "back" of the vector container.

#include<iostream>
#include<vector>
using namespace std;

vector<int> vecSalary;
int nSalary;
int main()
{
    while(cin>>nSalary)
    {
        vecSalary.push_back(nSalary);
    }
    auto it = vecSalary.begin();
    vecSalary.insert(it, 2200);
    cout<<"the size of the vector is : "<<vecSalary.size()<<endl;
}

Accessing data in the vector container

In essence, the vector container is an array, which is similar to accessing the elements in the array. We can also use the "[]" symbol to access the data in the vector container with the order of the elements in the container as a subscript.

// Assign 3000 to the first data element in the vecSalary container
vecSalary[0] = 3000;

However, in order to ensure the security of accessing elements, the vector container uses more iterators or at() functions to access the data elements in the container.

// Defines an index value variable for accessing data in a container
vector<int>::size_type nIndex = 0;
// Loop through the vector container
for( auto it = vecSalary.begin();
	it != vecSalary.end(); ++it, ++nIndex )
{
	// Read the data in the container through the iterator
	cout<<"The current salary is: "<<*it<<endl;
	// Modify the value of the element in the container through the at() function
	vecSalary.at( nIndex ) += 1000; // Pay rise! 1000 yuan per person!
	cout<<"After the salary increase: "<<*it<<endl;
}

Keywords: C++ STL

Added by lordfrikk on Fri, 04 Mar 2022 13:02:24 +0200