#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <string>
using namespace std;
int main(void)
{
vector<string> demo;
string word;
auto beg = demo.begin();
while (cin >> word)
{
beg = demo.insert(beg,word); //和deque的push_front效果一样
}
beg = demo.begin();
for(;beg != demo.end();beg++)
{
cout << *beg << " ";
}
cout << endl;
return 0;
}
上面函数中,insert会获取给出的迭代器的位置,并在指向那里,在指向位置的前面插入.并返回指向此元素的迭代器.
对了,对于vector容器调用insert后,之前定义的迭代器会失效,所以在while循环里对beg 进行了重新赋值.