Skip to content

Commit

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

**Description** : Copies elements from the range `[first, last)`, to another range beginning at `d_first`, omitting the elements which satisfy specific criteria.

**Example**:
```cpp
auto isOdd = [](int i) {
return ((i%2) == 1);
};

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

// Copy elements to destination that return false for isOdd
std::remove_copy_if(origin.begin(), //first
origin.end(), //last
std::back_inserter(destination), //d_first
isOdd);

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

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

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

int main()
{
auto isOdd = [](int i) {
return ((i%2) == 1);
};

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

// Copy elements to destination that return false for isOdd
std::remove_copy_if(origin.begin(), //first
origin.end(), //last
std::back_inserter(destination), //d_first
isOdd);

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

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

0 comments on commit d5b905c

Please sign in to comment.