功能描述:
对vector容器进行插入、删除操作函数原型:
push_back(ele); //尾部插入元素elepop_back(); //删除最后一个元素insert(const_iterator pos, ele); //迭代器指向位置pos插入元素eleinsert(const_iterator pos, int count,ele);//迭代器指向位置pos插入count个元素eleerase(const_iterator pos); //删除迭代器指向的元素erase(const_iterator start, const_iterator end);//删除迭代器从start到end之间的元素clear(); //删除容器中所有元素 #include <iostream> #include <vector> using namespace std; void printVector(vector<int>& v) { for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; } void test() { vector<int> v1; //尾插 v1.push_back(10); v1.push_back(20); v1.push_back(30); v1.push_back(40); v1.push_back(50); printVector(v1); //尾删 v1.pop_back(); printVector(v1); //插入 v1.insert(v1.begin(), 100); printVector(v1); v1.insert(v1.begin(), 2, 1000); printVector(v1); //清空 //v1.erase(v1.begin(), v1.end()); v1.clear(); printVector(v1); } int main() { test(); return 0; } //尾插 push_back() //尾删 pop_back() //插入 insert(位置迭代器) //删除 erase(位置迭代器) //清空 clear();C++vector容器vector插入和删除
