算法之旅,直奔<algorithm>之八 copy_n
来源:互联网 发布:2016上海程序员工资 编辑:程序博客网 时间:2024/05/24 04:23
copy_n(vs2010版本)
- 引言
这是我学习总结<algorithm>的第八篇,我觉得copy_n比copy在实际应用中更多一些。它指定了要复制的元素的个数。- 作用
copy_n 作用是将一些连续地址的数据的前n个元素复制到一个新的容器里,并返回容器里的剩下的元素的首向量。- 原理
template<class InputIterator, class Size, class OutputIterator> OutputIterator copy_n (InputIterator first, Size n, OutputIterator result){ while (n>0) { *result = *first; ++result; ++first; --n; } return result;}
- 实验
将数据集合{10,20,30,40,50,60,70}里的前五个元素复制到容器里,然后输出返回值,返回值为int初始默认值0,也就是容器里的第六个元素向量。
- 代码
#include <iostream> // std::cout#include <algorithm> // std::copy#include <vector> // std::vectorint main () {int myints[]={10,20,30,40,50,60,70};std::vector<int> myvector;myvector.resize(7); // allocate space for 7 elementsstd::cout<<*(std::copy_n ( myints, 5, myvector.begin() ))<<"\n";std::cout << "myvector contains:";for (std::vector<int>::iterator it = myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';system("pause");return 0;}
template<class InputIterator, class Size, class OutputIterator> OutputIterator copy_n (InputIterator first, Size n, OutputIterator result){ while (n>0) { *result = *first; ++result; ++first; --n; } return result;}
#include <iostream> // std::cout#include <algorithm> // std::copy#include <vector> // std::vectorint main () {int myints[]={10,20,30,40,50,60,70};std::vector<int> myvector;myvector.resize(7); // allocate space for 7 elementsstd::cout<<*(std::copy_n ( myints, 5, myvector.begin() ))<<"\n";std::cout << "myvector contains:";for (std::vector<int>::iterator it = myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';system("pause");return 0;}
0 0
- 算法之旅,直奔<algorithm>之八 copy_n
- 算法之旅,直奔<algorithm>之一 all_of
- 算法之旅,直奔<algorithm>之二 adjacent_find
- 算法之旅,直奔<algorithm>之三 any_of
- 算法之旅,直奔<algorithm>之四 binary_search
- 算法之旅,直奔<algorithm>之五 copy
- 算法之旅,直奔<algorithm>之六 copy_backward
- 算法之旅,直奔<algorithm>之七 copy_if
- 算法之旅,直奔<algorithm>之九 count
- 算法之旅,直奔<algorithm>之十 count_if
- 算法之旅,直奔<algorithm>之十一 equal
- 算法之旅,直奔<algorithm>之十二 equal_range
- 算法之旅,直奔<algorithm>之十三 fill
- 算法之旅,直奔<algorithm>之十四 fill_n
- 算法之旅,直奔<algorithm>之十五 find
- 算法之旅,直奔<algorithm>之十六 find_end
- 算法之旅,直奔<algorithm>之十七 find_first_of
- 算法之旅,直奔<algorithm>之十八 for_each
- asp获取当前页面url
- hive partition简介
- Introduction to JavaScipt
- asp语言中if判断语句的求助
- 黑苹果硬件支持列表【最新】2013.12
- 算法之旅,直奔<algorithm>之八 copy_n
- MyEclipse 快捷键
- MyEclipse 创建maven webProject
- 使用Spring的Quartz方式实现Java定时器功能
- oracle 数据库审计
- C++之一些事一些情--函数传参
- jquery mobile常用的data-role类型
- BZOJ 1257 [CQOI2007]余数之和sum 题解
- Emacs自定义配置(一):智能复制行,未选定时注释当前行,Copy后智能缩进