std::replace_copy_if() function with example in C++ STL

C++ STL | std::replace_copy_if() function: Here, we are going to learn about the replace_copy_if() function of algorithm header in C++ STL with example.
Submitted by IncludeHelp, on May 26, 2019

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.

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

Syntax of std::replace_copy_if() function

    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.
  • 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.
  • new_value – a value to be assigned instead of an 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.

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 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.

//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()




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.