1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <unordered_map>
10
11// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
12// class Alloc = allocator<pair<const Key, T>>>
13// class unordered_multimap
14
15// iterator erase(const_iterator p)
16
17#include <unordered_map>
18#include <string>
19#include <set>
20#include <cassert>
21#include <cstddef>
22
23#include "test_macros.h"
24#include "../../../check_consecutive.h"
25#include "min_allocator.h"
26
27struct TemplateConstructor
28{
29 template<typename T>
30 TemplateConstructor (const T&) {}
31};
32
33bool operator==(const TemplateConstructor&, const TemplateConstructor&) { return false; }
34struct Hash { std::size_t operator() (const TemplateConstructor &) const { return 0; } };
35
36int main(int, char**)
37{
38 {
39 typedef std::unordered_multimap<int, std::string> C;
40 typedef std::pair<int, std::string> P;
41 P a[] =
42 {
43 P(1, "one"),
44 P(2, "two"),
45 P(3, "three"),
46 P(4, "four"),
47 P(1, "four"),
48 P(2, "four"),
49 };
50 C c(a, a + sizeof(a)/sizeof(a[0]));
51 C::const_iterator i = c.find(x: 2);
52 C::const_iterator i_next = i;
53 ++i_next;
54 std::string es = i->second;
55 C::iterator j = c.erase(position: i);
56 assert(j == i_next);
57
58 assert(c.size() == 5);
59 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
60 Eq eq = c.equal_range(x: 1);
61 assert(std::distance(eq.first, eq.second) == 2);
62 std::multiset<std::string> s;
63 s.insert(x: "one");
64 s.insert(x: "four");
65 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s);
66 eq = c.equal_range(x: 2);
67 assert(std::distance(eq.first, eq.second) == 1);
68 C::const_iterator k = eq.first;
69 assert(k->first == 2);
70 assert(k->second == (es == "two" ? "four" : "two"));
71 eq = c.equal_range(x: 3);
72 assert(std::distance(eq.first, eq.second) == 1);
73 k = eq.first;
74 assert(k->first == 3);
75 assert(k->second == "three");
76 eq = c.equal_range(x: 4);
77 assert(std::distance(eq.first, eq.second) == 1);
78 k = eq.first;
79 assert(k->first == 4);
80 assert(k->second == "four");
81 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
82 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
83 }
84#if TEST_STD_VER >= 11
85 {
86 typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>,
87 min_allocator<std::pair<const int, std::string>>> C;
88 typedef std::pair<int, std::string> P;
89 P a[] =
90 {
91 P(1, "one"),
92 P(2, "two"),
93 P(3, "three"),
94 P(4, "four"),
95 P(1, "four"),
96 P(2, "four"),
97 };
98 C c(a, a + sizeof(a)/sizeof(a[0]));
99 C::const_iterator i = c.find(2);
100 C::const_iterator i_next = i;
101 ++i_next;
102 std::string es = i->second;
103 C::iterator j = c.erase(i);
104 assert(j == i_next);
105
106 assert(c.size() == 5);
107 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
108 Eq eq = c.equal_range(1);
109 assert(std::distance(eq.first, eq.second) == 2);
110 std::multiset<std::string> s;
111 s.insert("one");
112 s.insert("four");
113 CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
114 eq = c.equal_range(2);
115 assert(std::distance(eq.first, eq.second) == 1);
116 C::const_iterator k = eq.first;
117 assert(k->first == 2);
118 assert(k->second == (es == "two" ? "four" : "two"));
119 eq = c.equal_range(3);
120 assert(std::distance(eq.first, eq.second) == 1);
121 k = eq.first;
122 assert(k->first == 3);
123 assert(k->second == "three");
124 eq = c.equal_range(4);
125 assert(std::distance(eq.first, eq.second) == 1);
126 k = eq.first;
127 assert(k->first == 4);
128 assert(k->second == "four");
129 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
130 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
131 }
132#endif
133#if TEST_STD_VER >= 14
134 {
135 // This is LWG #2059
136 typedef TemplateConstructor T;
137 typedef std::unordered_multimap<T, int, Hash> C;
138 typedef C::iterator I;
139
140 C m;
141 T a{0};
142 I it = m.find(a);
143 if (it != m.end())
144 m.erase(it);
145 }
146#endif
147
148 return 0;
149}
150

source code of libcxx/test/std/containers/unord/unord.multimap/unord.multimap.modifiers/erase_const_iter.pass.cpp