Skip to content

Commit

Permalink
Documenting algorithm/replace_copy
Browse files Browse the repository at this point in the history
  • Loading branch information
thamara committed Sep 10, 2019
1 parent 2915364 commit 8acdd16
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
31 changes: 31 additions & 0 deletions algorithm/replace_copy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# replace_copy

**Description** : Copies elements from the range `[first, last)`, to another range beginning at `d_first`, replacing the elements that matches `old_value` by a `new_value`.

**Example**:
```cpp
std::vector<int> origin {3, 5, 3, 1, 2, 3};
std::vector<int> destination;

// Copy elements to destination replacing elements that are 3 by 0
std::replace_copy(origin.begin(), //first
origin.end(), //last
std::back_inserter(destination), //d_first
3, //old_value
0 //new_value
);

// origin is still {3, 5, 3, 1, 2, 3}
for (auto value : origin) {
std::cout << value << " ";
}
std::cout << std::endl;

// destination is {0, 5, 0, 1, 2, 0}
for (auto value : destination) {
std::cout << value << " ";
}
std::cout << std::endl;
```
**[See Sample code](../snippets/algorithm/replace_copy.cpp)**
**[Run Code](https://rextester.com/VQNU37190)**
36 changes: 36 additions & 0 deletions snippets/algorithm/replace_copy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Author : Thamara Andrade
Date : Date format 09/09/2019
Time : Time format 23:00
Description : Copies elements from a range to another replacing the ones that satisfies a criteria.
*/

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
std::vector<int> origin {3, 5, 3, 1, 2, 3};
std::vector<int> destination;

// Copy elements to destination replacing elements that are 3 by 0
std::replace_copy(origin.begin(), //first
origin.end(), //last
std::back_inserter(destination), //d_first
3, //old_value
0 //new_value
);

// origin is still {3, 5, 3, 1, 2, 3}
for (auto value : origin) {
std::cout << value << " ";
}
std::cout << std::endl;

// destination is {0, 5, 0, 1, 2, 0}
for (auto value : destination) {
std::cout << value << " ";
}
std::cout << std::endl;
}

0 comments on commit 8acdd16

Please sign in to comment.