C++ 算法 中的 copy函数

首页 / C++入门教程 / C++ 算法 中的 copy函数

C++算法copy()函数用于将集合[first,last]的所有元素复制到从结果开始的另一个集合中。

copy - 语法

template<class InputIterator, class OutputIterator>OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result);

copy - 参数

first:它是参数的第一个元素的输入迭代器,其中元素本身包含在参数中。

last:它是参数最后一个元素的输入迭代器,其中元素本身不包含在参数中。

result:它是新集合中要复制元素的第一个元素的输出迭代器。

copy - 返回值

返回以result为开头的新参数的最后一个元素的迭代器。

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-algorithm-copy-function.html

来源:LearnFk无涯教程网

copy - 例子1

#include<iostream>
#include<algorithm>
#include<vector>
int main()
{
	int newints[]={15,25,35,45,55,65,75};
	std::vector<int> newvector(7);
	std::copy (newints, newints+7, newvector.begin());
	std::cout <<"newvector contains:";
	for (std::vector<int>::iterator ti= newvector.begin(); ti!=newvector.end(); ++ti)
	std::cout<<" " <<*ti;
	std::cout<<"\n";
	return 0;
}

输出:

newvector contains: 15 25 35 45 55 65 75

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入拆解Java虚拟机 -〔郑雨迪〕

代码精进之路 -〔范学雷〕

如何做好一场技术演讲 -〔极客时间〕

SQL必知必会 -〔陈旸〕

容量保障核心技术与实战 -〔吴骏龙〕

Tony Bai · Go语言第一课 -〔Tony Bai〕

朱涛 · Kotlin编程第一课 -〔朱涛〕

Dubbo源码剖析与实战 -〔何辉〕

结构思考力 · 透过结构看思考 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)