Skip to content

Commit 2915364

Browse files
committed
Documenting algorithm/remove_if
1 parent d5b905c commit 2915364

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

algorithm/remove_if.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# remove_if
2+
3+
**Description** : Removes elementes from range `[first, last)` that satisfy a condition and returns a new past-the-end iterator for the new end of range.
4+
5+
**Example**:
6+
```cpp
7+
auto isOdd = [](int i) {
8+
return ((i%2) == 1);
9+
};
10+
11+
std::vector<int> v {1, 2, 3, 4, 5};
12+
13+
// Remove all elements that returns true for isOdd
14+
auto newEndIt = std::remove_if(v.begin(), v.end(), isOdd);
15+
16+
// Erase elements from [newEndIt, v.end()]
17+
v.erase(newEndIt, v.end());
18+
19+
// v is now {2, 4}
20+
for (auto value : v) {
21+
std::cout << value << " ";
22+
}
23+
```
24+
**[See Sample code](../snippets/algorithm/remove_if.cpp)**
25+
**[Run Code](https://rextester.com/OWAN21750)**

snippets/algorithm/remove_if.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Author : Thamara Andrade
3+
Date : Date format 09/09/2019
4+
Time : Time format 23:00
5+
Description : Removes elements from vector that satisfies a criteria.
6+
*/
7+
8+
#include <iostream>
9+
#include <vector>
10+
#include <algorithm>
11+
12+
int main()
13+
{
14+
auto isOdd = [](int i) {
15+
return ((i%2) == 1);
16+
};
17+
18+
std::vector<int> v {1, 2, 3, 4, 5};
19+
20+
// Remove all elements that returns true for isOdd
21+
auto newEndIt = std::remove_if(v.begin(), v.end(), isOdd);
22+
23+
// Erase elements from [newEndIt, v.end()]
24+
v.erase(newEndIt, v.end());
25+
26+
// v is now {2, 4}
27+
for (auto value : v) {
28+
std::cout << value << " ";
29+
}
30+
}

0 commit comments

Comments
 (0)