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

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

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.

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

Syntax of std::replace_copy() function

    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.
  • old_value – a value to 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:

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

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




Comments and Discussions!

Load comments ↻





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