-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_if_and_erase.cpp
53 lines (41 loc) · 976 Bytes
/
remove_if_and_erase.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
45
46
47
48
49
50
51
52
53
// Online C++ compiler to run C++ program online
#include <algorithm>
#include <string>
#include <string_view>
#include <iostream>
#include <cctype>
#include <vector>
#include <iostream>
void print_container(const std::vector<int>& c)
{
for (int i : c) {
std::cout << i << " ";
}
std::cout << '\n';
}
struct book
{
int price;
std::string name;
friend std::ostream& operator<<(std::ostream& os, const book& b)
{
os << "price: " << b.price << "; name: " << b.name << std::endl;
return os;
}
};
int main()
{
book a{12, "cook"};
std::vector<int> c{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
print_container(c);
auto cc = std::remove_if(c.begin(), c.end(), [](int i){return i % 2 == 0;});
print_container(c);
c.erase(cc, c.end());
print_container(c);
std::cout << a << std::endl;
}
// output:
// 0 1 2 3 4 5 6 7 8 9
// 1 3 5 7 9 5 6 7 8 9
// 1 3 5 7 9
// price: 12; name: cook