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

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