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_map
14
15// iterator erase(const_iterator p)
16
17#include <unordered_map>
18#include <string>
19#include <cassert>
20
21#include "test_macros.h"
22#include "min_allocator.h"
23
24struct TemplateConstructor
25{
26 template<typename T>
27 TemplateConstructor (const T&) {}
28};
29
30bool operator==(const TemplateConstructor&, const TemplateConstructor&) { return false; }
31struct Hash { std::size_t operator() (const TemplateConstructor &) const { return 0; } };
32
33int main(int, char**)
34{
35 {
36 typedef std::unordered_map<int, std::string> C;
37 typedef std::pair<int, std::string> P;
38 P a[] =
39 {
40 P(1, "one"),
41 P(2, "two"),
42 P(3, "three"),
43 P(4, "four"),
44 P(1, "four"),
45 P(2, "four"),
46 };
47 C c(a, a + sizeof(a)/sizeof(a[0]));
48 C::const_iterator i = c.find(x: 2);
49 c.erase(position: i);
50 assert(c.size() == 3);
51 assert(c.at(1) == "one");
52 assert(c.at(3) == "three");
53 assert(c.at(4) == "four");
54 }
55#if TEST_STD_VER >= 11
56 {
57 typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>,
58 min_allocator<std::pair<const int, std::string>>> C;
59 typedef std::pair<int, std::string> P;
60 P a[] =
61 {
62 P(1, "one"),
63 P(2, "two"),
64 P(3, "three"),
65 P(4, "four"),
66 P(1, "four"),
67 P(2, "four"),
68 };
69 C c(a, a + sizeof(a)/sizeof(a[0]));
70 C::const_iterator i = c.find(2);
71 c.erase(i);
72 assert(c.size() == 3);
73 assert(c.at(1) == "one");
74 assert(c.at(3) == "three");
75 assert(c.at(4) == "four");
76 }
77#endif
78#if TEST_STD_VER >= 14
79 {
80 // This is LWG #2059
81 typedef TemplateConstructor T;
82 typedef std::unordered_map<T, int, Hash> C;
83 typedef C::iterator I;
84
85 C m;
86 T a{0};
87 I it = m.find(a);
88 if (it != m.end())
89 m.erase(it);
90 }
91#endif
92
93 return 0;
94}
95

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