std :: copy()函数以及C ++ STL中的示例
C++STLstd::copy()函数
copy()函数是算法标头的库函数,用于复制容器的元素,它将容器的元素从给定范围复制到给定起始位置的另一个容器。
注意:要使用copy()函数–包括<algorithm>头文件,或者您可以简单地使用<bits/stdc++。h>头文件。
std::copy()函数的语法
std::copy(iterator source_first, iterator source_end, iterator target_start);
参数:
迭代器source_first,迭代器source_end–是源容器的迭代器位置。
迭代器target_start–是目标容器的开始迭代器。
返回值:迭代器–它是复制到元素的目标范围末尾的迭代器。
示例
Input:
//declaring & initializing an int array
int arr[] = { 10, 20, 30, 40, 50 };
//向量声明
vector<int> v1(5);
//复制数组元素到向量
copy(arr, arr + 5, v1.begin());
Output:
//如果我们打印值
arr: 10 20 30 40 50
v1: 10 20 30 40 50C++STL程序演示std::copy()函数的使用
在此示例中,我们将数组元素复制到向量。
//C++STL程序演示使用
//std::copy()函数
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
//declaring & initializing an int array
int arr[] = { 10, 20, 30, 40, 50 };
//向量声明
vector<int> v1(5);
//复制数组元素到向量
copy(arr, arr + 5, v1.begin());
//打印数组
cout << "arr: ";
for (int x : arr)
cout << x << " ";
cout << endl;
//打印载体
cout << "v1: ";
for (int x : v1)
cout << x << " ";
cout << endl;
return 0;
}输出结果
arr: 10 20 30 40 50 v1: 10 20 30 40 50
参考:C++std::copy()
热门推荐
10 生日祝福语男生韩语简短
11 伴娘祝福语简短朋友的话
12 婆婆去世的祝福语简短
13 新婚宴祝福语简短大全
14 婚礼伴娘祝福语大全简短
15 给新生的简短祝福语
16 给孩子祝福语简短创意
17 鼠年同事祝福语简短创意
18 立冬祝福语简短给女孩