1. 通过 push_back() 尾部增加一个元素 :
vector 可以通过 “push_back ” 写入数据,通过 push_back 可以将数据直接写入至 vector 的末尾,push_back 会自动申请内存,并且多次 push_back 后会自动预先分配内存,如下代码:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 //第一部分:容器 vector 9 vector<int> num; 10 11 cout << "num 的元素个数:" << num.size() << endl; 12 cout << "num 容器的大小:" << num.capacity() << endl; 13 14 num.push_back(1); //push_back:往 vector 最后放置1个元素 “1” 15 num.push_back(2); 16 num.push_back(3); 17 num.push_back(4); 18 num.push_back(3); 19 20 cout << "尾部插入5个元素后" << endl; 21 cout << "num 的元素个数:" << num.size() << endl; 22 cout << "num 容器的大小:" << num.capacity() << endl; 23 24 return 0; 25 }
运行结果:
插入5个元素后打印内存大小,结果发现这时的 vector 占用了6个 int 元素的内存
2.通过 pop_back() 删除尾部的一个元素:
vector 可以通过 “pop_back” 删除尾部一个元素,pop_back 删除尾部的元素后,元素个数会变少,但空间并不会缩小,如下代码:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 vector<int>num(5, 888); 9 10 printf("使用 resize 之前\n"); 11 cout << "使用 resize 之前 num 的元素数量:" << num.size() << endl; 12 cout << "使用 resize 之前 num 的空间大小:" << num.capacity() << endl; 13 14 for (int i = 0; i < num.size(); i++) 15 { 16 cout << num[i] << endl; 17 } 18 19 num.pop_back(); 20 21 cout << "使用 resize 之后 num 的元素数量:" << num.size() << endl; 22 cout << "使用 resize 之后 num 的空间大小:" << num.capacity() << endl; 23 for (int i = 0; i < num.size(); i++) 24 { 25 cout << num[i] << endl; 26 } 27 28 return 0; 29 }
打印结果:
3. 使用下标进行数据修改:
如果使用下标进行数据写入,需要一个前提,那就是 vector 已经进行了内存分配,如果像下方代码:vector 没有进行带参构造,这时不能直接通过下标去写 vector 的数据。
1 vector<int> num; 2 3 num[0] = 1; //不能这样通过下标去访问,因为vector是进行默认构造的,这时并没有相关内存
以下几种带参构造的 vector 是可以进行下标数据写入的:
//例1 int main() { vector<int> num(10); num[0] = 1; num[1] = 2; return 0; } //例2 int main() { vector<int> num(10); vector<int>num_1(num); num_1[0] = 1; num_1[1] = 2; return 0; } //例3 int main() { int test[] = { 1,2,3,4,5 }; vector<int> num(test, test + 2); num[0] = 1; num[1] = 2; return 0; }
4. 使用 vectorName.at() 进行数据修改:
这种用法与下标修改类似,at() 这个方法返回的是一个引用,可以吧一个常量 int 赋值给它,如下代码:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 vector<int>num(5, 111); 9 10 printf("使用 resize 之前\n"); 11 cout << "使用 resize 之前 num 的元素数量:" << num.size() << endl; 12 cout << "使用 resize 之前 num 的空间大小:" << num.capacity() << endl; 13 14 for (int i = 0; i < num.size(); i++) 15 { 16 cout << num[i] << endl; 17 } 18 19 num.at(1) = 222; 20 num.at(2) = 333; 21 num.at(3) = 444; 22 23 cout << "使用 resize 之后 num 的元素数量:" << num.size() << endl; 24 cout << "使用 resize 之后 num 的空间大小:" << num.capacity() << endl; 25 for (int i = 0; i < num.size(); i++) 26 { 27 cout << num[i] << endl; 28 } 29 30 return 0; 31 }
打印结果:
5. 使用 assign 进行重写操作
assign 可以改变原来的 vector 中的元素个数和值,如下代码:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 vector<int>num(10, 666); 9 10 printf("使用 assign 之前\n"); 11 cout << "使用 assign 之前 num 的元素数量:" << num.size() << endl; 12 cout << "使用 assign 之前 num 的空间大小:" << num.capacity() << endl; 13 14 num[0] = 1; 15 num[1] = 2; 16 17 for (int i = 0; i < num.size(); i++) 18 { 19 cout << num[i] << endl; 20 } 21 num.assign(2,888); //第一种 assign 的用法 22 printf("使用 assign 之后\n"); 23 cout << "使用 assign 之后 num 的元素数量:" << num.size() << endl; 24 cout << "使用 assign 之后 num 的空间大小:" << num.capacity() << endl; 25 for (int i = 0; i < num.size(); i++) 26 { 27 cout << num[i] << endl; 28 } 29 30 return 0; 31 }
打印结果:
我们会发现,使用 assign 之后元素数量变为了2,但 vector 的空间大小并没有变。
assign 还可以搭配迭代器用:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 vector<int>num(10, 111); 9 vector<int>num_1(10, 888); 10 11 printf("使用 assign 之前\n"); 12 cout << "使用 assign 之前 num_1 的元素数量:" << num_1.size() << endl; 13 cout << "使用 assign 之前 num_1 的空间大小:" << num_1.capacity() << endl; 14 15 num_1[0] = 1; 16 num_1[1] = 2; 17 18 for (int i = 0; i < num_1.size(); i++) 19 { 20 cout << num_1[i] << endl; 21 } 22 num_1.assign(2,888); //第一种 assign 的用法 23 num_1.assign(num.begin() + 3, num.end()); //第二种 配合迭代器的用法 24 25 printf("assign 的第二种用法:\n"); 26 cout << "assign 的第二种用法之后 num_1 的元素数量:" << num_1.size() << endl; 27 cout << "assign 的第二种用法之后 num_1 的空间大小:" << num_1.capacity() << endl; 28 for (int i = 0; i < num_1.size(); i++) 29 { 30 cout << num_1[i] << endl; 31 } 32 33 return 0; 34 }
打印如下:
既然使用迭代器可以这样玩,那么使用数组也一样可以,如下:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 int test[] = { 111,222,333,444,555,666,777,888,999 }; 9 vector<int>num(10, 888); 10 11 printf("使用 assign 之前\n"); 12 cout << "使用 assign 之前 num_1 的元素数量:" << num.size() << endl; 13 cout << "使用 assign 之前 num_1 的空间大小:" << num.capacity() << endl; 14 15 num[0] = 1; 16 num[1] = 2; 17 18 for (int i = 0; i < num.size(); i++) 19 { 20 cout << num[i] << endl; 21 } 22 num.assign(2,888); //第一种 assign 的用法 23 num.assign(test, test + 5); //第二种 使用迭代器一样的用法 用数组进行重新赋值 24 25 printf("assign 的第二种用法:\n"); 26 cout << "assign 的第二种用法之后 num_1 的元素数量:" << num.size() << endl; 27 cout << "assign 的第二种用法之后 num_1 的空间大小:" << num.capacity() << endl; 28 for (int i = 0; i < num.size(); i++) 29 { 30 cout << num[i] << endl; 31 } 32 33 return 0; 34 }
打印如下:
6. 获取&修改 vector 容器的第一个和最后一个元素的值:
获取:使用 vectorName.front() 与 vectorName.back() 来获取 vector 的第一个元素与最后一个元素的引用,如下代码:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 int test[] = { 111,222,333,444,555,666,777,888,999 }; 9 vector<int>num(test, test + 9); 10 11 cout << "num 的第一个元素为:" << num.front() << endl; 12 cout << "num 的第一个元素为:" << num.back() << endl; 13 14 return 0; 15 }
打印结果:
赋值:因为返回的是其引用,那么我们也可以进行赋值操作,如下代码:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 int test[] = { 111,222,333,444,555,666,777,888,999 }; 9 vector<int>num(test, test + 9); 10 11 cout << "num 的第一个元素为:" << num.front() << endl; 12 cout << "num 的第一个元素为:" << num.back() << endl; 13 14 cout << "=========改变首尾的值========" << endl; 15 num.front() = 1; 16 num.back() = 9; 17 18 for (int i = 0; i < num.size(); i++) 19 { 20 cout << num.at(i) << endl; 21 } 22 23 return 0; 24 }
打印结果:
=======================================================================================================================