C++ algorithm replace_copy() function
Example
Create a copy of a vector where "Ford" is replaced with "Toyota":
vector<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
vector<string> newcars(4);
replace_copy(cars.begin(), cars.end(), newcars.begin(), (string)"Ford", (string)"Toyota");
for (string car : newcars) {
cout << car << " ";
}
Try it Yourself »
Definition and Usage
The replace_copy()
function creates a copy of a data range where all occurrences of a value are replaced with a different value.
The range of data is specified by iterators.
Syntax
replace_copy(iterator start, iterator end, iterator destination, <type> find, <type> replace);
<type>
refers to the type of the data that the range contains.
Parameter Values
Parameter | Description |
---|---|
start | Required. An iterator pointing to the start of the data range being copied. |
end | Required. An iterator pointing to the end of the data range being copied. Elements up to this position will be copied, but the element at this position will not be included. |
destination | Required. An iterator pointing to the start of the data range that the data will be copied to. |
find | Required. The value to search for. |
replace | Required. The replacement value. |
Technical Details
Returns: | An iterator pointing to the end of the destination data range. |
---|
Related Pages
Read more about data structures in our Data Structures Tutorial.
Read more about iterators in our Iterators Tutorial.
Read more about algorithms in our Algorithms Tutorial.