-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_rm_elems.cpp
More file actions
35 lines (27 loc) · 774 Bytes
/
Copy pathlist_rm_elems.cpp
File metadata and controls
35 lines (27 loc) · 774 Bytes
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
/*
remove all elements from a list meeting some condition using erase and remove_if.
see: https://en.cppreference.com/w/cpp/algorithm/remove
*/
#include <iostream>
#include <list>
#include <algorithm>
#include <cstdlib>
using std::cout;
int RandInt() { return(std::rand()%100); }
int main()
{
std::srand(97);
// create a list of randomly generated ints, then print
int n = 13;
std::list<int> aList (n);
std::generate(aList.begin(), aList.end(), RandInt);
aList.sort();
for (auto& e: aList)
cout << e << " ";
cout << "\n";
// remove values less than 20
aList.erase(std::remove_if(aList.begin(), aList.end(), [](int x){return x<20;}), aList.end());
for (auto& e: aList)
cout << e << " ";
cout << "\n";
}