Detailed explanation of C++ vector vector

Vector is a vector type. It can hold many types of data, such as several integers, so it is called a container. This article introduces how to use it.
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, and the header file needs to be included to use it:

code show as below:

#include<vector>;
   
   
  • 1

1. Initialization of vector: There are five ways, examples are as follows:

1vector<int> a(10);

//定义了10个整型元素的向量(尖括号中为元素类型名,它可以是任何合法的数据类型),但没有给出初值,其值是不确定的。
2vector<int> a(10,1);   

 //定义了10个整型元素的向量,且给出每个元素的初值为1
3vector<int> a(b);       

//用b向量来创建a向量,整体复制性赋值
4vector<int> a(b.begin(),b.begin+3);    

 //定义了a值为b中第0个到第2个(共3个)元素
5int b[7]={1,2,3,4,5,9,8};
     vector<int> a(b,b+7);        

 //从数组中获得初值
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

Second, several important operations of the vector object, examples are as follows:

1a.assign(b.begin(), b.begin()+3);     

  //b为向量,将b的0~2个元素构成的向量赋给a 
2a.assign(4,2);      

 //是a只含4个元素,且每个元素为2
3a.back();           

//返回a的最后一个元素
4a.front();         

//返回a的第一个元素
5a[i];            

 //返回a的第i个元素,当且仅当a[i]存在2013-12-07
6a.clear();        

//清空a中的元素
7a.empty();       

 //判断a是否为空,空则返回ture,不空则返回false
8a.pop_back();   

 //删除a向量的最后一个元素
9a.erase(a.begin()+1,a.begin()+3);      

 //删除a中第1个(从第0个算起)到第2个元素,也就是说删除的元素从a.begin()+1算起(包括它)一直到a.begin()+ 3(不包括它)
10a.push_back(5);    

//在a的最后一个向量后插入一个元素,其值为5
11a.insert(a.begin()+1,5);    

//在a的第1个元素(从第0个算起)的位置插入数值5,如a1,2,3,4,插入元素后为1,5,2,3,4
12a.insert(a.begin()+1,3,5);     

//在a的第1个元素(从第0个算起)的位置插入3个数,其值都为5
13a.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
14a.size();      

//返回a中元素的个数;
15a.capacity();  

//返回a在内存中总共可以容纳的元素个数
16a.rezize(10);  

//将a的现有元素个数调至10个,多则删,少则补,其值随机
17a.rezize(10,2);    
//将a的现有元素个数调至10个,多则删,少则补,其值为2
18a.reserve(100);    

//将a的容量(capacity)扩充至100,也就是说现在测试a.capacity();的时候返回值是100.这种操作只有在需要给a添加大量数据的时候才 显得有意义,因为这将避免内存多次容量扩充操作(当a的容量不足时电脑会自动扩容,当然这必然降低性能) 
19a.swap(b);      

//b为向量,将a中的元素和b中的元素进行整体性交换
20a==b;          

//b为向量,向量的比较操作还有!=,>=,<=,>,<
   
   
  • 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
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

3. There are several ways to access the vector sequentially. Examples are as follows:

1. Add an element to the vector a

code show as below:

vector<int> a;

for(int i = 0;i < 10;i++)
{
    a.push_back(i);
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2. You can also select elements from the array to add to the vector

code show as below:

int a[6]={1,2,3,4,5,6};

vector<int> b;

for(int i = 1;i <= 4;i++)
{
    b.push_back(a[i]);
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3. You can also select elements from an existing vector to add to the vector

code show as below:

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);
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

4. You can also read elements from the file and add them to the vector

code show as below:

ifstream in("data.txt");

vector<int> a;

for(int i; in >> i)
{
    a.push_back(i);
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

5. 【Misunderstanding】

code show as below:

vector<int> a;
for(int i = 0;i < 10;i++)
{
    a[i]=i;
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5

// This and similar practices are wrong. I also made this mistake at the beginning, and later found that the subscript can only be used to get the existing element , and now a[i] is still an empty object

2 read elements from the vector
1) read by subscript

code show as below:

int a[6]={1,2,3,4,5,6};

vector<int> b(a,a+4);

for(int i = 0;i <= b.size() - 1;i++)
{
    cout<<b[i]<<" ";
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2) Read through the traverser

code show as below:

int a[6]={1,2,3,4,5,6};

vector<int> b(a,a+4);

for(vector<int>::iterator it  = b.begin();it !=  b.end();it++)
{
    cout<<*it<<" ";
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Fourth, several important algorithms need to include header files when using:

#include<algorithm>
   
   
  • 1
1)sort(a.begin(),a.end());

 //对a中的从a.begin()(包括它)到a.end()(不包括它)的元素进行从小到大排列
2)reverse(a.begin(),a.end()); 

//对a中的从a.begin()(包括它)到a.end()(不包括它)的元素倒置,但不排列,如a中元素为1,3,2,4,倒置后为4,2,3,1
3)copy(a.begin(),a.end(),b.begin()+1); 

//把a中的从a.begin()(包括它)到a.end()(不包括它)的元素复制到b中,从b.begin()+1的位置(包括它)开始复制,覆盖掉原有元素
4)find(a.begin(),a.end(),10); 

//在a中的从a.begin()(包括它)到a.end()(不包括它)的元素中查找10,若存在返回其在向量中的位置
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Reprinted from: https://blog.csdn.net/jenny_84/article/details/45009381

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325600115&siteId=291194637