This article describes the C++ dynamic array class (std::vector), which extends ordinary arrays in a more convenient way and can easily handle variable-length arrays.
What is std::vector?
std::vector
is a class that allows you to freely change the length of an array. With ordinary arrays, the size of the array must be specified at the beginning, and it is not possible to change the size of the array later. For example, int a[10] and the size of the array must also be declared at the beginning, and the size of the array cannot be changed later. However, std::vector
can be used to make this possible. It is also much more convenient than ordinary arrays in that it can refer to the number of array elements, etc.
How to use std::vector (from include to array declaration)
Include~vector declaration
To use std::vector
, you need to include it as follows. In this article, the namespace std is specified in order to write "vector" from std::vector. When declaring a vector, declare it as vector<type name> variable name. At this time, when specifying the size of the array in advance, it is written as vector<type name> variable name (size). To initialize the array to a specified value, use vector<type name> variable name (size, value to be initialized).
#include <vector> using namespace std;//Declare namespace int main() { vector<int> x;//If no namespace is specified, then std::vector<int> x; vector<int> x(3); vector<double> x(3,3.14); }
Declaration of two-dimensional arrays
To declare a two-dimensional array using vector, write vector<vector<type name>> variable name (size, value to be initialized). The last line vector<vector<int>>> xy(3, vector<string>(3, "abc")); stores all the letters "abc" in a 3 x 3 matrix as shown in the table below.
vector<vector<string>> xy; vector<vector<string>> xy(3); vector<vector<string>> xy(3, vector<string>(3)); vector<vector<string>> xy(3, vector<string>(3, "abc"));
int | 0 | 1 | 2 |
0 | abc | abc | abc |
1 | abc | abc | abc |
2 | abc | abc | abc |
How to use std::vector (array processing)
Value References & Assignments
Once a vector has been declared for referencing and assigning values, the rest can be done in the same way as for ordinary array processing.
タイトル:vector1.cpp
#include<iostream> #include<string> #include <vector> using namespace std; int main() { vector<vector<string>> xy(3, vector<string>(3,"abc")); cout<< xy[1][1]<< endl; xy[1][1]="def"; cout<< xy[1][1]<< endl; return 0; }
タイトル:output
abc def
Browse & change the number of elements
To refer to the number of elements in a vector, use variable name.size(). In the case of a two-dimensional array, variable name.size() can be used to obtain the number of rows, and variable name[0].size() can be used to obtain the number of columns. The number of array elements can also be changed by setting variable.resize(number of elements to be changed).
タイトル:vector1.cpp
#include<iostream> #include<string> #include <vector> using namespace std; int main() { vector<string> x(3, "abc");//1*3 vector<vector<string>> xy(4, vector<string>(3,"abc"));//4*3 cout << x.size() << endl; //3 x.resize(5);//Changed to 5 cout << x.size() << endl; //5 cout<< xy.size()<< endl; //4 cout<< xy[1].size()<< endl;//3 xy.resize(5); //Changed to 5*5 for(int i=0; i<5; i++){ xy[i].resize(5); } cout<< xy.size() << endl; //5 cout<< xy[0].size() << endl; //5 return 0; }
タイトル:output
3 5 4 3 5 5
Adding an element to the end of an array
It is also possible to add an element to the end of an array by using vector's push_back(). For example, x.push_back(3) will add the number 3 to the end of the array x.
タイトル:pushback1.cpp
#include<iostream> #include<string> #include <vector> using namespace std; int main() { vector<string> x(3, "abc");//1*3 x.push_back("def"); for(int i=0;i<x.size();i++){ cout << x[i] << endl; } return 0; }
タイトル:output
abc abc def
The addition of an element to the tail of a two-dimensional array is described as follows.
タイトル:pushback2.cpp
#include<iostream> #include<string> #include <vector> using namespace std; int main() { vector<vector<string>> xy(3, vector<string>(3,"abc"));//3*3 xy[0].push_back("def"); for(int i=0;i<xy.size();i++){ for(int j=0;j<xy[i].size();j++){ cout << xy[i][j]<< " "; } cout << endl; } return 0; }
タイトル:output
abc abc abc def abc abc abc abc abc abc
Deleting Elements
In vector, you can delete elements at specified locations by using the variable name.erase
(specified location).
タイトル:erase.cpp
#include<iostream> #include<string> #include <vector> using namespace std; int main() { vector<string> x={"abc","def","ghi"};//1*3 x.erase(x.begin()+1);//+1st from the top for(int i=0;i<x.size();i++){ cout << x[i] << " "; } cout << endl; x.erase(x.end()-1);//end for(int i=0;i<x.size();i++){ cout << x[i] << " "; } cout << endl; return 0; }
タイトル:output
abc ghi abc
Conclusion
This article introduced the usage of std::vector. Here is a summary of the main points.
- std::vector is a C++ dynamic array class that extends ordinary arrays to be more convenient
- Once declared, it can be treated as an ordinary array.
- Allows referencing and changing the number of array elements
- Elements can be added to the end of the array
- Allows deletion of elements at specified locations
Please try using C++ std::vector, a more convenient extension of ordinary arrays!