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

stl中copy()函数

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

replace_copy() function is a library function of algorithm header, it is used to replace value in the range and copy the elements in the result with replaced values, it accepts a range [start, end] of the sequence, beginning position of the result sequence, old and new value and it replaces the all old_value with the new_value in the range and copies the range to the range beginning at result.

replace_copy()函数算法标头的库函数,用于替换范围内的值并用替换后的值复制结果中的元素,它接受序列的范围[开始,结束]结果的开始位置序列,旧值和新值,它将范围内的所有old_value替换为new_value ,并将范围复制到从结果开始的范围。

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

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

Syntax of std::replace_copy() function

std :: replace_copy()函数的语法

    std::replace_copy(
        iterator start, 
        iterator end, 
        iterator start_result, 
        const T& old_value, 
        const T& new_value);

Parameter(s):

参数:

  • iterator start, iterator end – these are the iterators pointing to the starting and ending positions in the container, where we have to run the replace operation.

    迭代器开始,迭代器结束 –这些迭代器指向容器中我们必须运行替换操作的开始和结束位置。

  • iterator start_result – is the beginning iterator of the result sequence.

    迭代器start_result –是结果序列的开始迭代器。

  • old_value – a value to be replaced.

    old_value –要替换的值。

  • new_value – a value to be assigned instead of an old_value.

    new_value –要分配的值,而不是old_value。

Return value: iterator – it returns an iterator pointing to the element that follows the last element which is written in the result sequence.

返回值: iterator –它返回一个迭代器,该迭代器指向在结果序列中写入的最后一个元素之后的元素。

Example:

例:

    Input:
    //an array (source)
    int arr[] = { 10, 20, 30, 10, 20, 30, 40, 50, 60 };
    
    //declaring a vector (result sequence)
    vector<int> v(6);
    
    //copying 6 elements of array to vector
    //by replacing 10 to -1
    replace_copy(arr + 0, arr + 6, v.begin(), 10, -1);
    
    Output:
    vector (v): -1 20 30 -1 20 30

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

In this program, we have an array and we are copying its 6 elements to a vector by replacing 10 with -1.

在此程序中,我们有一个数组,并通过将-10替换为-1将其6个元素复制到向量中。

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

int main()
{
    //an array
    int arr[] = { 10, 20, 30, 10, 20, 30, 40, 50, 60 };

    //declaring a vector
    vector<int> v(6);

    //printing  elements
    cout << "before replacing, Array (arr): ";
    for (int x : arr)
        cout << x << " ";
    cout << endl;

    cout << "vector (v): ";
    for (int x : v)
        cout << x << " ";
    cout << endl;

    //copying 6 elements of array to vector
    //by replacing 10 to -1
    replace_copy(arr + 0, arr + 6, v.begin(), 10, -1);

    //printing vector elements
    cout << "after replacing, Array (arr): ";
    for (int x : arr)
        cout << x << " ";
    cout << endl;

    cout << "vector (v): ";
    for (int x : v)
        cout << x << " ";
    cout << endl;

    return 0;
}

Output

输出量

before replacing, Array (arr): 10 20 30 10 20 30 40 50 60
vector (v): 0 0 0 0 0 0
after replacing, Array (arr): 10 20 30 10 20 30 40 50 60
vector (v): -1 20 30 -1 20 30

Reference: C++ std::replace_copy()

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

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

stl中copy()函数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值