Home > Article > Backend Development > C++ vector container function usage example
This article mainly talks about the use of vector container functions in C. It has certain reference value. Interested friends can learn about it.
Vector (vector) is a sequential container that encapsulates a dynamically sized array. Like any other type of container, it can store various types of objects. You can simply think of a vector as a dynamic array that can store any type. The following is an example of using vector:
#include <iostream> #include <string> #include<vector> using namespace std; int main() { vector<int>vec_a{ 1, 2, 3, 4, 5 }; vector<int>vec_b{ 1, 2, 3, 4, 5 }; //返回容器的数目(容器大小) int a=vec_a.size(); //交换容器里的内容; vec_b.swap(vec_a); //将元素(7)添加到矢量末尾 vec_a.push_back(7); //返回一个指向容器中第一个元素的迭代器; vector<int>::iterator it = vec_a.begin(); //返回一个表示超过容器威迭代器 vector<int>::iterator it0 = vec_a.end(); //erase()接受两个参数,删除定义参数之间的元素。 //区间[it,it+3)由迭代器it指定,不包括(it+3); vec_a.erase(it, it + 3); //删除单个; vec_a.erase(it); //在(it+3)位置插入3; vec_a.insert(it+3, 4); }
Related tutorials: C Video tutorial
The above is the detailed content of C++ vector container function usage example. For more information, please follow other related articles on the PHP Chinese website!