1.代码示例
// back_inserter example
#include <iostream> // std::cout
#include <iterator> // std::back_inserter
#include <vector> // std::vector
#include <algorithm> // std::copy
using namespace std;
int main () {
std::vector<int> foo,bar;
for (int i=1; i<=5; i++){
foo.push_back(i);
bar.push_back(i*10);
}
for(int i = 0; i < foo.size(); i++)
cout <<" foo: "<< foo[i];
cout << endl;
for(int i = 0; i < bar.size(); i++)
cout <<" bar: "<< bar[i];
cout << endl;
//将bar的vector数据插入到foo后边
std::copy (bar.begin(),bar.end(),back_inserter(foo));
// for(int i = 0; i < foo.size(); i++)
// cout <<" foo: "<< foo[i];
// cout << endl;
std::cout << "foo contains:";
for ( std::vector<int>::iterator it = foo.begin(); it!= foo.end(); ++it )
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}