一、向vector容器中增添元素
1、在末尾增添一个元素 push_back()
2、在任意地方插入一个或多个元素 insert()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<vector> // 注意这里不用.h using namespace std; int main() { vector<int> demo {1,2,3,4}; // 在末尾增添一个元素 demo.push_back(5); for(auto c : demo) cout << c << " "; cout << endl; // 在下标为2的位置,插入了9 demo.insert(demo.begin() + 2,9); for(auto c : demo) cout << c << " "; cout << endl; // 在下标为2的位置,插入了3个6 demo.insert(demo.begin() + 2,3,6); for(auto c : demo) cout << c << " "; cout << endl; return 0; } 运行结果如下: 1 2 3 4 5 1 2 9 3 4 5 1 2 6 6 6 9 3 4 5具体例子
标签:题中,demo,元素,back,C++,算法,include From: https://www.cnblogs.com/algorithm-code/p/16717439.html