-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveIf.cpp
44 lines (32 loc) · 1.12 KB
/
RemoveIf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* \file RemoveIf.cpp
* \brief Removes from the container all the elements for which Predicate pred returns true
*
* This calls the destructor of these objects and reduces the container size by
* the number of elements removed.
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
std::list<int> listOfInts( {2, 3, 4, 6, 4, 9, 1, 2, 8, 9, 4, 6, 2} );
std::cout << "Before: " << STD_TRACE_VAR(listOfInts.size()) << std::endl;
// Remove only first occurrence of element with value 4
auto pred = [](const int &val) -> bool
{
return (val >= 2 && val < 5);
};
listOfInts.remove_if(pred);
std::cout << "After: " << STD_TRACE_VAR(listOfInts.size()) << std::endl;
std::cout << listOfInts << std::endl;
std::cout << std::endl;
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
Before: listOfInts.size(): 13
After: listOfInts.size(): 6
std::list (size=6): {6,9,1,8,9,6}
#endif