stl中copy()函数_std :: copy()函数以及C ++ STL中的示例

stl中copy()函数

C ++ STL std :: copy()函数 (C++ STL std::copy() function)

copy() function is a library function of algorithm header, it is used to copy the elements of a container, it copies the elements of a container from given range to another container from a given beginning position.

copy()函数算法标头的库函数,用于复制容器的元素,它将容器的元素从给定范围复制到给定起始位置的另一个容器。

Note:To use copy() function – include <algorithm> header or you can simple use <bits/stdc++.h> header file.

注意:要使用copy()函数 –包括<algorithm>头文件,或者您可以简单地使用<bits / stdc ++。h>头文件。

Syntax of std::copy() function

std :: copy()函数的语法

    std::copy(iterator source_first, iterator source_end, iterator target_start);

Parameter(s):

参数:

  • iterator source_first, iterator source_end – are the iterator positions of the source container.

    迭代器source_first,迭代器source_end –是源容器的迭代器位置。

  • iterator target_start – is the beginning iterator of the target container.

    迭代器target_start –是目标容器的开始迭代器。

Return value: iterator – it is an iterator to the end of the target range where elements have been copied.

返回值: 迭代器 –它是目标元素已复制到目标范围末尾的迭代器。

Example:

例:

    Input:
    //declaring & initializing an int array
    int arr[] = { 10, 20, 30, 40, 50 };
    
    //vector declaration
    vector<int> v1(5);
    
    //copying array elements to the vector
    copy(arr, arr + 5, v1.begin());

    Output:
    //if we print the value
    arr: 10 20 30 40 50
    v1: 10 20 30 40 50

C ++ STL程序演示了std :: copy()函数的使用 (C++ STL program to demonstrate use of std::copy() function)

In this example, we are copying the array elements to the vector.

在此示例中,我们将数组元素复制到向量。

//C++ STL program to demonstrate use of
//std::copy() function
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    //declaring & initializing an int array
    int arr[] = { 10, 20, 30, 40, 50 };
    //vector declaration
    vector<int> v1(5);

    //copying array elements to the vector
    copy(arr, arr + 5, v1.begin());

    //printing array
    cout << "arr: ";
    for (int x : arr)
        cout << x << " ";
    cout << endl;

    //printing vector
    cout << "v1: ";
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    return 0;
}

Output

输出量

arr: 10 20 30 40 50
v1: 10 20 30 40 50

Reference: C++ std::copy()

参考: C ++ std :: copy()

翻译自: https://www.includehelp.com/stl/std-copy-function-with-example.aspx

stl中copy()函数

std::copystd::move 都是 C++ STL 算法函数,它们的作用是不同的。 std::copy 用于将一个序列的元素复制到另一个序列,它会复制元素的值,而不会改变原序列的元素。它的函数签名如下: ```cpp template <class InputIterator, class OutputIterator> OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result); ``` std::move 也用于将一个序列的元素复制到另一个序列,但它会将元素的值“移动”到新序列,而不是复制。这意味着,原序列的元素可能会被修改或销毁。它的函数签名如下: ```cpp template <class InputIterator, class OutputIterator> OutputIterator move (InputIterator first, InputIterator last, OutputIterator result); ``` 下面是一个使用 std::copystd::move 的示例代码: ```cpp #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v1 = {1, 2, 3, 4, 5}; std::vector<int> v2(5); // 存储复制结果的 vector // 使用 std::copy 复制 v1 的元素到 v2 std::copy(v1.begin(), v1.end(), v2.begin()); // 使用 std::move 将 v1 的元素移动到 v3 std::vector<int> v3 = std::move(v1); // 输出 v2 的元素 for (int i : v2) { std::cout << i << " "; } std::cout << std::endl; // 输出 v3 的元素 for (int i : v3) { std::cout << i << " "; } std::cout << std::endl; return 0; } ``` 输出结果为: ``` 1 2 3 4 5 1 2 3 4 5 ``` 在上面的代码,我们使用 std::copy 函数将 v1 的元素复制到 v2 ,并使用 std::move 将 v1 的元素移动到 v3 。可以看到,v2 的元素与 v1 的元素相同,而 v3 的元素与 v1 的元素不同。这是因为 std::move 函数将 v1 的元素“移动”到了 v3 ,所以 v1 的元素已经被修改或销毁。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值