本文主要是介绍C++ back_inserter(插入迭代器)用法(九),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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;
}
这篇关于C++ back_inserter(插入迭代器)用法(九)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!