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// <map>
10
11// class map
12
13// iterator erase(const_iterator first, const_iterator last);
14
15#include <map>
16#include <cassert>
17
18#include "test_macros.h"
19#include "min_allocator.h"
20
21int main(int, char**)
22{
23 {
24 typedef std::map<int, double> M;
25 typedef std::pair<int, double> P;
26 typedef M::iterator I;
27 P ar[] =
28 {
29 P(1, 1.5),
30 P(2, 2.5),
31 P(3, 3.5),
32 P(4, 4.5),
33 P(5, 5.5),
34 P(6, 6.5),
35 P(7, 7.5),
36 P(8, 8.5),
37 };
38 M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
39 assert(m.size() == 8);
40 I i = m.erase(first: m.cbegin(), last: m.cbegin());
41 assert(m.size() == 8);
42 assert(i == m.begin());
43 assert(m.begin()->first == 1);
44 assert(m.begin()->second == 1.5);
45 assert(std::next(m.begin())->first == 2);
46 assert(std::next(m.begin())->second == 2.5);
47 assert(std::next(m.begin(), 2)->first == 3);
48 assert(std::next(m.begin(), 2)->second == 3.5);
49 assert(std::next(m.begin(), 3)->first == 4);
50 assert(std::next(m.begin(), 3)->second == 4.5);
51 assert(std::next(m.begin(), 4)->first == 5);
52 assert(std::next(m.begin(), 4)->second == 5.5);
53 assert(std::next(m.begin(), 5)->first == 6);
54 assert(std::next(m.begin(), 5)->second == 6.5);
55 assert(std::next(m.begin(), 6)->first == 7);
56 assert(std::next(m.begin(), 6)->second == 7.5);
57 assert(std::next(m.begin(), 7)->first == 8);
58 assert(std::next(m.begin(), 7)->second == 8.5);
59
60 i = m.erase(first: m.cbegin(), last: std::next(x: m.cbegin(), n: 2));
61 assert(m.size() == 6);
62 assert(i == m.begin());
63 assert(std::next(m.begin(), 0)->first == 3);
64 assert(std::next(m.begin(), 0)->second == 3.5);
65 assert(std::next(m.begin(), 1)->first == 4);
66 assert(std::next(m.begin(), 1)->second == 4.5);
67 assert(std::next(m.begin(), 2)->first == 5);
68 assert(std::next(m.begin(), 2)->second == 5.5);
69 assert(std::next(m.begin(), 3)->first == 6);
70 assert(std::next(m.begin(), 3)->second == 6.5);
71 assert(std::next(m.begin(), 4)->first == 7);
72 assert(std::next(m.begin(), 4)->second == 7.5);
73 assert(std::next(m.begin(), 5)->first == 8);
74 assert(std::next(m.begin(), 5)->second == 8.5);
75
76 i = m.erase(first: std::next(x: m.cbegin(), n: 2), last: std::next(x: m.cbegin(), n: 6));
77 assert(m.size() == 2);
78 assert(i == std::next(m.begin(), 2));
79 assert(std::next(m.begin(), 0)->first == 3);
80 assert(std::next(m.begin(), 0)->second == 3.5);
81 assert(std::next(m.begin(), 1)->first == 4);
82 assert(std::next(m.begin(), 1)->second == 4.5);
83
84 i = m.erase(first: m.cbegin(), last: m.cend());
85 assert(m.size() == 0);
86 assert(i == m.begin());
87 assert(i == m.end());
88 }
89#if TEST_STD_VER >= 11
90 {
91 typedef std::map<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
92 typedef std::pair<int, double> P;
93 typedef M::iterator I;
94 P ar[] =
95 {
96 P(1, 1.5),
97 P(2, 2.5),
98 P(3, 3.5),
99 P(4, 4.5),
100 P(5, 5.5),
101 P(6, 6.5),
102 P(7, 7.5),
103 P(8, 8.5),
104 };
105 M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
106 assert(m.size() == 8);
107 I i = m.erase(m.cbegin(), m.cbegin());
108 assert(m.size() == 8);
109 assert(i == m.begin());
110 assert(m.begin()->first == 1);
111 assert(m.begin()->second == 1.5);
112 assert(std::next(m.begin())->first == 2);
113 assert(std::next(m.begin())->second == 2.5);
114 assert(std::next(m.begin(), 2)->first == 3);
115 assert(std::next(m.begin(), 2)->second == 3.5);
116 assert(std::next(m.begin(), 3)->first == 4);
117 assert(std::next(m.begin(), 3)->second == 4.5);
118 assert(std::next(m.begin(), 4)->first == 5);
119 assert(std::next(m.begin(), 4)->second == 5.5);
120 assert(std::next(m.begin(), 5)->first == 6);
121 assert(std::next(m.begin(), 5)->second == 6.5);
122 assert(std::next(m.begin(), 6)->first == 7);
123 assert(std::next(m.begin(), 6)->second == 7.5);
124 assert(std::next(m.begin(), 7)->first == 8);
125 assert(std::next(m.begin(), 7)->second == 8.5);
126
127 i = m.erase(m.cbegin(), std::next(m.cbegin(), 2));
128 assert(m.size() == 6);
129 assert(i == m.begin());
130 assert(std::next(m.begin(), 0)->first == 3);
131 assert(std::next(m.begin(), 0)->second == 3.5);
132 assert(std::next(m.begin(), 1)->first == 4);
133 assert(std::next(m.begin(), 1)->second == 4.5);
134 assert(std::next(m.begin(), 2)->first == 5);
135 assert(std::next(m.begin(), 2)->second == 5.5);
136 assert(std::next(m.begin(), 3)->first == 6);
137 assert(std::next(m.begin(), 3)->second == 6.5);
138 assert(std::next(m.begin(), 4)->first == 7);
139 assert(std::next(m.begin(), 4)->second == 7.5);
140 assert(std::next(m.begin(), 5)->first == 8);
141 assert(std::next(m.begin(), 5)->second == 8.5);
142
143 i = m.erase(std::next(m.cbegin(), 2), std::next(m.cbegin(), 6));
144 assert(m.size() == 2);
145 assert(i == std::next(m.begin(), 2));
146 assert(std::next(m.begin(), 0)->first == 3);
147 assert(std::next(m.begin(), 0)->second == 3.5);
148 assert(std::next(m.begin(), 1)->first == 4);
149 assert(std::next(m.begin(), 1)->second == 4.5);
150
151 i = m.erase(m.cbegin(), m.cend());
152 assert(m.size() == 0);
153 assert(i == m.begin());
154 assert(i == m.end());
155 }
156#endif
157
158 return 0;
159}
160

source code of libcxx/test/std/containers/associative/map/map.modifiers/erase_iter_iter.pass.cpp