vector Usage

介绍

在c++中,vector像是一种动态数组,可以自由更改大小,有时候也被称为容器。
使用vector前需要包含#include <vector>头文件

使用方法

初始化

int是指容器的类型,abc是容器的名称。

1
vector<int>abc;

常用内置函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//b为向量,将b的0-2个元素赋值给向量a
a.assign(b.begin(),b.begin()+3);
//a含有4个值为2的元素
a.assign(4,2);
//返回a的最后一个元素
a.back();
//返回a的第一个元素
a.front();
//返回a的第i元素,当且仅当a存在
a[i];
//清空a中的元素
a.clear();
//判断a是否为空,空则返回true,非空则返回false
a.empty();
//删除a向量的最后一个元素
a.pop_back();
//删除a中第一个(从第0个算起)到第二个元素,也就是说删除的元素从a.begin()+1算起(包括它)一直到a.begin()+3(不包括它)结束
a.erase(a.begin()+1,a.begin()+3);
//在a的末尾插入一个元素,其值为i
a.push_back(i);
//在a的第一个元素(从第0个算起)位置插入数值5,
a.insert(a.begin()+1,5);
//在a的第一个元素(从第0个算起)位置插入3个数,其值都为5
a.insert(a.begin()+1,3,5);
//b为数组,在a的第一个元素(从第0个元素算起)的位置插入b的第三个元素到第5个元素(不包括b+6)
a.insert(a.begin()+1,b+3,b+6);
//返回a中元素的个数
a.size();
//返回a在内存中总共可以容纳的元素个数
a.capacity();
//将a的现有元素个数调整至10个,多则删,少则补,其值随机
a.resize(10);
//将a的现有元素个数调整至10个,多则删,少则补,其值为2
a.resize(10,2);
//将a的容量扩充至100,
a.reserve(100);
//b为向量,将a中的元素和b中的元素整体交换
a.swap(b);
//b为向量,向量的比较操作还有 != >= > <= <
a==b;

常用算法

1
2
3
4
5
6
7
8
9
#include<algorithm>
//对a中的从a.begin()(包括它)到a.end()(不包括它)的元素进行从小到大排列
sort(a.begin(),a.end());
//对a中的从a.begin()(包括它)到a.end()(不包括它)的元素倒置,但不排列,如a中元素为1,3,2,4,倒置后为4,2,3,1
reverse(a.begin(),a.end());
//把a中的从a.begin()(包括它)到a.end()(不包括它)的元素复制到b中,从b.begin()+1的位置(包括它)开始复制,覆盖掉原有元素
copy(a.begin(),a.end(),b.begin()+1);
//在a中的从a.begin()(包括它)到a.end()(不包括它)的元素中查找10,若存在返回其在向量中的位置
find(a.begin(),a.end(),10);

Introduction

In C++, a vector is like a dynamic array that can change its size freely and is sometimes referred to as a container.
To use vector, you need to include the #include <vector> header file.

Usage

Initialization

int refers to the type of the container, and abc is the name of the container.

1
vector<int>abc;

Common Built-in Functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//b is a vector, assign the first 3 elements of b to vector a
a.assign(b.begin(),b.begin()+3);
//a contains 4 elements with value 2
a.assign(4,2);
//return the last element of a
a.back();
//return the first element of a
a.front();
//return the i-th element of a, if a exists
a[i];
//clear all elements in a
a.clear();
//check if a is empty, return true if empty, false otherwise
a.empty();
//delete the last element of vector a
a.pop_back();
//delete elements from the first (index 0) to the third element of a, meaning it deletes elements starting from a.begin()+1 (inclusive) up to a.begin()+3 (exclusive)
a.erase(a.begin()+1,a.begin()+3);
//insert an element with value i at the end of a
a.push_back(i);
//insert value 5 at the first element position (from index 0) of a
a.insert(a.begin()+1,5);
//insert 3 numbers, all with value 5, at the first element position (from index 0) of a
a.insert(a.begin()+1,3,5);
//b is an array, insert the third to the fifth element of b (excluding b+6) at the first element position (from index 0) of a
a.insert(a.begin()+1,b+3,b+6);
//return the number of elements in a
a.size();
//return the total number of elements a can hold in memory
a.capacity();
//adjust the existing number of elements in a to 10, deleting excess or filling with random values
a.resize(10);
//adjust the existing number of elements in a to 10, deleting excess or filling with value 2
a.resize(10,2);
//expand the capacity of a to 100
a.reserve(100);
//b is a vector, swap all elements between a and b
a.swap(b);
//b is a vector, comparison operations for vectors also include != >= > <= <
a==b;

Common Algorithms

1
2
3
4
5
6
7
8
9
#include<algorithm>
//sort the elements in a from a.begin() (inclusive) to a.end() (exclusive) in ascending order
sort(a.begin(),a.end());
//reverse the elements in a from a.begin() (inclusive) to a.end() (exclusive), but do not sort. Example: if a contains 1,3,2,4, after reversal it becomes 4,2,3,1
reverse(a.begin(),a.end());
//copy elements from a.begin() (inclusive) to a.end() (exclusive) in a to b, starting at position b.begin()+1 (inclusive), overwriting existing elements
copy(a.begin(),a.end(),b.begin()+1);
//search for 10 in elements from a.begin() (inclusive) to a.end() (exclusive) in a, return its position in the vector if found
find(a.begin(),a.end(),10);

vector Usage
https://tokisaki.top/blog/cpp-vector/
作者
Tokisaki Galaxy
发布于
2020年9月4日
许可协议