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

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