Wiki
vector container
Existing English translations were reused. 1 current source blocks remain in Simplified Chinese, without triggering paid translation.
1.vector container
1.1.Basic concepts of vector
Function:
- The vector data structure is very similar toan array and is also called asingle-ended array
Difference between vector and ordinary arrays:
- The difference is that an array is static space, while a vector candynamically expand
Dynamic scaling:
- It does not append a new space after the original space; instead, it finds a larger memory space, then copies the original data to the new space and releases the original space.

- The iterator of a vector container is an iterator that supports random access.
1.2.vector constructor
Function description:
- Create a vector container
Function prototype:
vector<T> v;//Implemented using a template class, default constructorvector(v.begin(), v.end());//Copies the elements in the range [begin(), end()) to itself.vector(n, elem);//The constructor copies n copies of elem to itself.vector(const vector &vec);//Copy constructor.
Example:
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int> v1; //无参构造
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
vector<int> v2(v1.begin(), v1.end());
printVector(v2);
vector<int> v3(10, 100);
printVector(v3);
vector<int> v4(v3);
printVector(v4);
}
int main() {
test01();
return 0;
}
Operation result:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100
**Summary:** The various construction methods of vector are not comparable; just use them flexibly.
1.3.vector assignment operation
Function description:
- Assign values to a vector container
Function prototype:
-
vector& operator=(const vector &vec);// Overload the assignment operator -
assign(beg, end);// Copy and assign the data in the interval [beg, end) to itself. -
assign(n, elem);// Copy and assign n copies of elem to itself.
Example:
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
//赋值操作
void test01()
{
vector<int> v1; //无参构造
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
vector<int>v2;
v2 = v1;
printVector(v2);
vector<int>v3;
v3.assign(v1.begin(), v1.end());
printVector(v3);
vector<int>v4;
v4.assign(10, 100);
printVector(v4);
}
int main() {
test01();
return 0;
}
Operation result:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
100 100 100 100 100 100 100 100 100 100
Summary: The vector assignment method is relatively simple; either using operator= or assign works.
1.4.vector capacity and size
Function description:
- Operations on the capacity and size of a vector container
Function prototype:
-
empty();// Determine whether the container is empty -
capacity();// Capacity of the container -
size();// Return the number of elements in the container -
resize(int num);// Respecify the length of the container as num. If the container becomes longer, fill the new positions with the default value. // If the container becomes shorter, the elements beyond the container length at the end are deleted.
-
resize(int num, elem);// Respecify the length of the container as num. If the container becomes longer, fill the new positions with the value elem. // If the container becomes shorter, the elements beyond the container length at the end are deleted
Example:
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int> v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
if (v1.empty())
{
cout << "v1为空" << endl;
}
else
{
cout << "v1不为空" << endl;
cout << "v1的容量 = " << v1.capacity() << endl;
cout << "v1的大小 = " << v1.size() << endl;
}
//resize 重新指定大小 ,若指定的更大,默认用0填充新位置,可以利用重载版本替换默认填充
v1.resize(15,10);
printVector(v1);
//resize 重新指定大小 ,若指定的更小,超出部分元素被删除
v1.resize(5);
printVector(v1);
}
int main() {
test01();
return 0;
}
Operation result:
0 1 2 3 4 5 6 7 8 9
v1不为空
v1的容量 = 16
v1的大小 = 10
0 1 2 3 4 5 6 7 8 9 10 10 10 10 10
0 1 2 3 4
Summary:
- Determine whether it is empty --- empty
- Return the number of elements --- size
- Return the container capacity --- capacity
- Respecify the size --- resize
1.5.Vector insertion and deletion
Function description:
- Insert and delete operations on vector containers
Function prototype:
push_back(ele);//Insert element ele at the endpop_back();//Delete the last elementinsert(const_iterator pos, ele);//Insert element ele at the position pointed to by the iterator posinsert(const_iterator pos, int count,ele);//Insert count copies of element ele at the position pointed to by the iterator poserase(const_iterator pos);//Delete the element pointed to by the iteratorerase(const_iterator start, const_iterator end);//Delete the elements between start and end pointed to by the iteratorsclear();//Delete all elements in the container
Example:
#include <iostream>
using namespace std;
#include <vector>
void printVector(vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
//插入和删除
void test01()
{
vector<int> v1;
//尾插
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
printVector(v1);
//尾删
v1.pop_back();
printVector(v1);
//插入
v1.insert(v1.begin(), 100);
printVector(v1);
v1.insert(v1.begin(), 2, 1000);
printVector(v1);
//删除
v1.erase(v1.begin());
printVector(v1);
//清空
v1.erase(v1.begin(), v1.end());
v1.clear();
printVector(v1);
}
int main() {
test01();
return 0;
}
Operation result:
10 20 30 40 50
10 20 30 40
100 10 20 30 40
1000 1000 100 10 20 30 40
1000 100 10 20 30 40
Summary:
- Push back --- push_back
- Pop back --- pop_back
- Insert --- insert (position iterator)
- Erase --- erase (position iterator)
- Clear --- clear
1.6.Vector data access
Function description:
- Access operations on data in vector
Function prototype:
at(int idx);//Return the data pointed to by index idxoperator[];//Return the data pointed to by index idxfront();//Return the first data element in the containerback();//Return the last data element in the container
Example:
#include <iostream>
#include <vector>
using namespace std;
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
for (int i = 0; i < v1.size(); i++)
{
cout << v1.at(i) << " ";
}
cout << endl;
cout << "v1的第一个元素为: " << v1.front() << endl;
cout << "v1的最后一个元素为: " << v1.back() << endl;
}
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
test01();
// 返回 0 表示程序正常结束。
return 0;
}
Operation result:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
v1的第一个元素为: 0
v1的最后一个元素为: 9
Summary:
- 除了用迭代器获取vector容器中元素,[ ]和at也可以
- front返回容器第一个元素
- back返回容器最后一个元素
1.7.vector container swap
Function description:
- Implement swapping of elements within two containers
Function prototype:
swap(vec);// Swap vec with its own elements
Example:
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
vector<int>v2;
for (int i = 10; i > 0; i--)
{
v2.push_back(i);
}
printVector(v2);
//互换容器
cout << "互换后" << endl;
v1.swap(v2);
printVector(v1);
printVector(v2);
}
void test02()
{
vector<int> v;
for (int i = 0; i < 100000; i++) {
v.push_back(i);
}
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
v.resize(3);
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
//收缩内存
vector<int>(v).swap(v); //匿名对象
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
}
int main() {
test01();
test02();
return 0;
}
Operation result:
0 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1
互换后
10 9 8 7 6 5 4 3 2 1
0 1 2 3 4 5 6 7 8 9
v的容量为:131072
v的大小为:100000
v的容量为:131072
v的大小为:3
v的容量为:3
v的大小为:3
Summary: swap can exchange two containers, which can achieve a practical effect of shrinking memory.
1.8.vector reserve space
Function description:
- Reduce the number of expansions when a vector dynamically expands its capacity.
Function prototype:
-
reserve(int len);// The container reserves a length of len elements. The reserved positions are not initialized, and the elements are inaccessible.
Example:
#include <iostream>
#include <vector>
using namespace std;
void test01()
{
vector<int> v;
//预留空间
v.reserve(100000);
int num = 0;
int* p = NULL;
for (int i = 0; i < 100000; i++) {
v.push_back(i);
if (p != &v[0]) {
p = &v[0];
num++;
}
}
cout << "num:" << num << endl;
}
int main() {
test01();
return 0;
}
Operation result:
num:1
Summary: If the data volume is large, you can use reserve to preallocate space from the start.