-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionalErase.cpp
More file actions
46 lines (37 loc) · 1.16 KB
/
ConditionalErase.cpp
File metadata and controls
46 lines (37 loc) · 1.16 KB
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
45
46
/**
* \file ConditionalErase.cpp
* \brief List Conditional Erase While Iteration
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//-------------------------------------------------------------------------------------------------
int main()
{
// Create a list and initialize it with 10 elements
std::list<int> listOfInts { 2, 3, 3, 4, 8, 9, 4, 6, 8, 3 };
// Iterate over the list using Iterators and erase elements
// which are multiple of 3 while iterating through list
for (auto it = listOfInts.begin(); it != listOfInts.cend(); ) {
// Remove elements while iterating
if ((*it) % 3 == 0) {
// erase() makes the passed iterator invalid
// But returns the iterator to the next of deleted element
it = listOfInts.erase(it);
} else {
++ it;
}
}
// Iterate over the list using for_each & Lambda Function and display contents
std::for_each(listOfInts.cbegin(), listOfInts.cend(),
[](const int val) -> void
{
std::cout << val << ",";
});
std::cout << std::endl;
return 0;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
2,4,8,4,8,
#endif