C++

复制容器(container)的元素至还有一个容器


本文地址: http://blog.csdn.net/caroline_wendy


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 





原文地址:https://www.cnblogs.com/zsychanpin/p/6807525.html