vector容器的成员函数insert()把一个或多个对象插入到iterator指示的位置。所查找的元素将出现在iterator指出的位置之前。
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <list>
using namespace std;
voidPrintInt(const int&nData)
{
cout<<nData<<endl;
}
int_tmain(int argc, _TCHAR* argv[])
{
vector<int> vecInt;
for(int i=0; i<10;++i)
{
vecInt.push_back(i);
}
vecInt.insert(vecInt.begin(),-1);
vecInt.insert(vecInt.end(),10);
int nArray[3] = {11,12,13};
//插入指定范围的对象
vecInt.insert(vecInt.end(),&nArray[0],&nArray[3]);
cout<<"插入元素后向量中的内容为:"<<endl;
for_each(vecInt.begin(),vecInt.end(),PrintInt);
return 0;
}
执行结果:

本文通过一个示例展示了如何使用C++ STL中的vector容器的insert()成员函数来在指定位置插入一个或多个元素。示例代码演示了在vector的不同位置插入单一元素和从数组中插入一系列元素的方法。
1056

被折叠的 条评论
为什么被折叠?



