C++ copy()
C++ 算法函数copy()
C++ 算法copy()函数用于从结果开始将容器[first,last]的所有元素复制到另一个容器中。
语法
template<class InputIterator, class OutputIterator>OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result);
参数
first: 它是范围的第一个元素的输入迭代器,元素本身包含在范围中。
last: 它是范围的last元素的输入迭代器,其中元素本身不包含在范围中。
result: 它是新容器中要复制元素的第一个元素的输出迭代器。
返回值
返回以结果开头的新范围的last元素的迭代器。
示例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
复杂度
该函数的复杂度从第一个元素到last元素都是线性的。
数据竞争
访问了部分或全部容器对象。
异常
如果任何容器元素抛出一个异常,该函数将引发异常。