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

stl中copy()函数

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

replace_copy_if() function is a library function of algorithm header, it is used to replace value in the given range and copy the elements in the result sequence with replaced values, it accepts a range [start, end] of the sequence, beginning position of the result sequence, a unary function (that will be used to validate the elements) and new value, it replaces the all elements which pass the test case applied in unary function (i.e. if function returns true) with the new_value in the range and copies the range to the beginning at result sequence.

replace_copy_if()函数算法标头的库函数,用于替换给定范围内的值,并用替换后的值复制结果序列中的元素,它接受序列的范围[start,end],起始位置结果序列,一元函数(将用于验证元素)和新值,它将通过一元函数(即,如果函数返回true)通过测试用例的所有元素替换为范围中的new_value并复制结果序列开头的范围。

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

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

Syntax of std::replace_copy_if() function

std :: replace_copy_if()函数的语法

    std::replace_copy_if(
        iterator start, 
        iterator end, 
        iterator start_result, 
        unary_function, 
        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 –是结果序列的开始迭代器。

  • unary_function – is a unary function that will perform the condition check on all elements in the given range and the elements which pass the condition will be replaced.

    unary_function –是一元函数,它将对给定范围内的所有元素执行条件检查,并且将替换通过条件的元素。

  • 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:

例:

    //function to test EVEN numbers
    bool isEVEN(int x){
        if (x % 2 == 0) return 1;
        else    return 0;
    }

    Input:
    //an array (source)
    int arr[] = { 10, 11, 12, 13, 14, 15, 100, 200, 300 };
    
    //declaring a vector (result sequence)
    vector<int> v(6);
    
    //copying 6 elements of array to vector
    //by replacing 10 to -1
    replace_copy_if(arr + 0, arr + 6, v.begin(), isEVEN, -1);
    
    Output:
    vector (v): -1 11 -1 13 -1 15

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

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

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

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

//function to test EVEN numbers
bool isEVEN(int x)
{
    if (x % 2 == 0)
        return 1;
    else
        return 0;
}

//main code
int main()
{
    //an array
    int arr[] = { 10, 11, 12, 13, 14, 15, 100, 200, 300 };

    //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_if(arr + 0, arr + 6, v.begin(), isEVEN, -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 11 12 13 14 15 100 200 300
vector (v): 0 0 0 0 0 0
after replacing, Array (arr): 10 11 12 13 14 15 100 200 300
vector (v): -1 11 -1 13 -1 15

Reference: C++ std::replace_copy_if()

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

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

stl中copy()函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值