C++復制容器(container)元素, 可以使用標准庫(STL)的copy()和copy_n()函數.
函數樣式: OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
代碼:
/*
* main.cpp
*
* Created on: 2014年6月17日
* Author: Spike
*/
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;
int main (void)
{
std::vector<int> vi = {1, 2, 3, 4, 0, 0, 0, 8, 9, 10};
std::vector<int> vb = {5, 6, 7};
std::vector<int>::iterator it = vi.begin()+4;
std::copy(vb.begin(), vb.end(), it);
std::copy(vi.begin(), vi.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
}
輸出:
1 2 3 4 5 6 7 8 9 10
作者:csdn博客 Spike_King