当前位置:首页 >> 编程开发 >> Visual C++ >> 内容

C++:复制容器(container)的元素至另一个容器

时间:2014/11/16 作者:平凡之路 来源:xuhantao.com 浏览:

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

更多精彩内容:http://www.bianceng.cn/Programming/cplus/

相关文章
  • 没有相关文章
  • 徐汉涛(www.xuhantao.com) © 2024 版权所有 All Rights Reserved.
  • 部分内容来自网络,如有侵权请联系站长尽快处理 站长QQ:965898558(广告及站内业务受理) 网站备案号:蒙ICP备15000590号-1