Sequence container in STL-vector (vector)

Sequence container in STL-vector (vector)

Vector is a vector type, it can hold many types of data, such as several integers, so it is called a container.
vector is an important member of the C++ STL, you need to include the header file when using it:

#include <vector>;

The length of the vector container is not fixed and can be changed dynamically when the program is running , so it is also called a dynamic array.

1. Initialization of vector

There are the following ways, examples are as follows:

vector<int> a;
vector<int> a(10); //定义了10个整型元素的向量(尖括号中为元素类型名,它可以是任何合法的数据类型),但没有给出初值,其值是不确定的。
vector<int> a(10,1); //定义了10个整型元素的向量,且给出每个元素的初值为1
vector<int> a(b); //用b向量来创建a向量,整体复制性赋值
vector<int> a(b.begin(),b.begin+3); //定义了a值为b中第0个到第2个(共3个)元素
int b[7]={
    
    1,2,3,4,5,9,8};vector<int> a(b,b+7); //从数组中获得初值

Two, the important operations of the vector object

Listed as follows:

a.assign(b.begin(), b.begin()+3); //b为向量,将b的0~2个元素构成的向量赋给a
a.assign(4,2); //是a只含4个元素,且每个元素为2
a.back(); //返回a的最后一个元素
a.front(); //返回a的第一个元素
a[i]; //返回a的第i个元素,当且仅当a[i]存在
a.at(i); //返回位置n处的元素。a.at(i)与a[i]差不多
a.clear(); //清空a中的元素
a.empty(); //判断a是否为空,空则返回ture,不空则返回false
a.erase(a.begin()+1,a.begin()+3); //删除a中第1个(从第0个算起)到第2个元素,也就是说删除的元素从a.begin()+1算起(包括它)一直到a.begin()+ 3(不包括它);可以只有一个参数,删除所在位置的值
a.push_back(5);//在a的最后一个向量后插入一个元素,其值为5
a.push_front(5);//在a的最前一个向量前插入一个元素,其值为5
a.pop_front();//从容器的首部移走数据;
a.pop_back();//从容器的尾部移走数据;
a.insert(a.begin()+1,5); //在a的第1个元素(从第0个算起)的位置插入数值5,如a为1,2,3,4,插入元素后为1,5,2,3,4
a.insert(a.begin()+1,3,5); //在a的第1个元素(从第0个算起)的位置插入3个数,其值都为5
a.insert(a.begin()+1,b+3,b+6); //b为数组,在a的第1个元素(从第0个算起)的位置插入b的第3个元素到第5个元素(不包括b+6),如b为1,2,3,4,5,9,8 ,插入元素后为1,4,5,9,2,3,4,5,9,8
a.emplace(a.begin+2,5); //在position处插入一个新的元素
a.emplace_back(); //在最后插入一个元素
a.size(); //返回a中元素的个数
a.max_size(); //返回最大容量
a.capacity(); //返回a在内存中总共可以容纳的元素个数
a.resize(10); //将a的现有元素个数调至10个,多则删,少则补,其值随机
a.resize(10,2); //将a的现有元素个数调至10个,多则删,少则补,其值为2
a.reserve(100); //将a的容量(capacity)扩充至100,也就是说现在测试a.capacity();的时候返回值是100.这种操作只有在需要给a添加大量数据的时候才 显得有意义,因为这将避免内存多次容量扩充操作(当a的容量不足时电脑会自动扩容,当然这必然降低性能) 
a.swap(b); //b为向量,将a中的元素和b中的元素进行整体性交换
a==b; //b为向量,向量的比较操作还有!=,>=,<=,>,<,对于==来说,首先比较它们的大小,若size相等,则依次比较元素;对于其他运算符,依次比较元素

Three, the way to access vector sequentially

Examples are as follows:

//向量a中添加元素
vector<int> a;
for(int i=0;i<10;i++)
a.push_back(i);

//也可以从数组中选择元素向向量中添加
int a[6]={
    
    1,2,3,4,5,6};
vector<int> b;
for(int i=1;i<=4;i++)
b.push_back(a[i]);

//也可以从现有向量中选择元素向向量中添加
int a[6]={
    
    1,2,3,4,5,6};
vector<int> b;
vector<int> c(a,a+4);
for(vector<int>::iterator it=c.begin();it!=c.end();it++)
b.push_back(*it);

//也可以从文件中读取元素向向量中添加
ifstream in("data.txt");
vector<int> a;
for(int i; in>>i)
a.push_back(i);

Four, several important algorithms

//使用时需要包含头文件:
#include<algorithm>
//算法如下:
sort(a.begin(),a.end()); //对a中的从a.begin()(包括它)到a.end()(不包括它)的元素进行从小到大排列
reverse(a.begin(),a.end()); //对a中的从a.begin()(包括它)到a.end()(不包括它)的元素倒置,但不排列,如a中元素为1,3,2,4,倒置后为4,2,3,1
copy(a.begin(),a.end(),b.begin()+1); //把a中的从a.begin()(包括它)到a.end()(不包括它)的元素复制到b中,从b.begin()+1的位置(包括它)开始复制,覆盖掉原有元素
find(a.begin(),a.end(),10); //在a中的从a.begin()(包括它)到a.end()(不包括它)的元素中查找10,若存在返回其在向量中的位置

Guess you like

Origin blog.csdn.net/hyl1181/article/details/108555610