-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherase_if_demo.cpp
132 lines (108 loc) · 3.76 KB
/
erase_if_demo.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// Demos MSVC STL implementation for https://wg21.link/P1209R0
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <unordered_map>
// To demonstrate deprecation
//#include <experimental/string>
struct is_vowel
{
bool operator()(const char c) const
{
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
};
struct is_odd
{
bool operator()(const int i) const
{
return i % 2 != 0;
}
};
struct is_first_odd
{
bool operator()(const std::pair<const int, int>& p) const
{
return p.first % 2 != 0;
}
};
template<typename T>
void print_list(const char* message, const T& list)
{
std::cout << message << ": { ";
for (auto const& element : list)
{
std::cout << element << " ";
}
std::cout << " }; size = " << list.size() << std::endl;
}
template<typename T>
void print_map(const char* message, const T& map)
{
std::cout << message << ": { ";
for (auto const& pair : map)
{
std::cout << "{" << pair.first << ", " << pair.second << "} ";
}
std::cout << " }; size = " << map.size() << std::endl;
}
void erase_demo()
{
// NOTE: the examples here are compiled against VS 16.6, which didn't include an implementation for P1115R3: https://wg21.link/P1115R3
// P1115R3 requires erase() and erase_if() to return the number of removed elements
// GH-566 (https://github.com/microsoft/STL/pull/566) fixes that and the fix will be included in VS 16.7.
// TODO: Update the samples to account for return of erase() and erase_if()
std::cout << "\nerase() demo\n";
std::string str{ "visual studio" };
// NOTE: std::experimental::erase() has been deprecated. If used in C++20, a warning like this will be issued:
// Error C4996 'std::experimental::fundamentals_v2::erase': warning STL4026 : std::experimental::erase() and std::experimental::erase_if()
// are deprecated by Microsoft and will be REMOVED. They are superseded by std::erase() and std::erase_if().
// You can define _SILENCE_EXPERIMENTAL_ERASE_DEPRECATION_WARNING to acknowledge that you have received this warning.
//std::experimental::erase(str, 'u');
std::cout << "Before erase: \"" << str << "\"; size = " << str.size() << std::endl;
std::erase(str, 'u');
std::cout << "After erase: \"" << str << "\"; size = " << str.size() << std::endl;
std::vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1 };
print_list("Before erase", v);
std::erase(v, 4);
print_list("After erase", v);
std::list<int> l{ 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1 };
print_list("Before erase", l);
std::erase(l, 4);
print_list("After erase", l);
}
void erase_if_demo()
{
std::cout << "\nerase_if() demo\n";
std::string str{ "cute fluffy kittens" };
std::cout << "Before erase_if: \"" << str << "\"; size = " << str.size() << std::endl;
std::erase_if(str, is_vowel{});
std::cout << "After erase_if: \"" << str << "\"; size = " << str.size() << std::endl;
std::vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1 };
print_list("Before erase_if", v);
std::erase_if(v, is_odd{});
print_list("After erase_if", v);
std::set<int> s{ 1, 2, 3, 4, 5, 6, 7 };
print_list("Before erase_if", s);
std::erase_if(s, is_odd{});
print_list("After erase_if", s);
std::map<int, int> m{ {1, 10}, {2, 20}, {3, 30}, {4, 40}, {5, 50}, {6, 60}, {7, 70} };
print_map("Before erase_if", m);
std::erase_if(m, is_first_odd{});
print_map("After erase_if", m);
std::unordered_map<int, int> um{ {1, 10}, {2, 20}, {3, 30}, {4, 40}, {5, 50}, {6, 60}, {7, 70} };
print_map("Before erase_if", um);
std::erase_if(um, is_first_odd{});
print_map("After erase_if", um);
}
void consistent_container_erasure_demo()
{
std::cout << "\nerase() and erase_if() demo:\n";
erase_demo();
erase_if_demo();
}