llvm-project
143 строки · 4.6 Кб
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{
24typedef std::multiset<int> M;
25typedef int V;
26typedef M::iterator I;
27V ar[] =
28{
291,
302,
313,
324,
335,
346,
357,
368
37};
38M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
39assert(m.size() == 8);
40I i = m.erase(std::next(m.cbegin(), 5), std::next(m.cbegin(), 5));
41assert(m.size() == 8);
42assert(i == std::next(m.begin(), 5));
43assert(*std::next(m.begin(), 0) == 1);
44assert(*std::next(m.begin(), 1) == 2);
45assert(*std::next(m.begin(), 2) == 3);
46assert(*std::next(m.begin(), 3) == 4);
47assert(*std::next(m.begin(), 4) == 5);
48assert(*std::next(m.begin(), 5) == 6);
49assert(*std::next(m.begin(), 6) == 7);
50assert(*std::next(m.begin(), 7) == 8);
51
52i = m.erase(std::next(m.cbegin(), 3), std::next(m.cbegin(), 4));
53assert(m.size() == 7);
54assert(i == std::next(m.begin(), 3));
55assert(*std::next(m.begin(), 0) == 1);
56assert(*std::next(m.begin(), 1) == 2);
57assert(*std::next(m.begin(), 2) == 3);
58assert(*std::next(m.begin(), 3) == 5);
59assert(*std::next(m.begin(), 4) == 6);
60assert(*std::next(m.begin(), 5) == 7);
61assert(*std::next(m.begin(), 6) == 8);
62
63i = m.erase(std::next(m.cbegin(), 2), std::next(m.cbegin(), 5));
64assert(m.size() == 4);
65assert(i == std::next(m.begin(), 2));
66assert(*std::next(m.begin(), 0) == 1);
67assert(*std::next(m.begin(), 1) == 2);
68assert(*std::next(m.begin(), 2) == 7);
69assert(*std::next(m.begin(), 3) == 8);
70
71i = m.erase(std::next(m.cbegin(), 0), std::next(m.cbegin(), 2));
72assert(m.size() == 2);
73assert(i == std::next(m.begin(), 0));
74assert(*std::next(m.begin(), 0) == 7);
75assert(*std::next(m.begin(), 1) == 8);
76
77i = m.erase(m.cbegin(), m.cend());
78assert(m.size() == 0);
79assert(i == m.end());
80}
81#if TEST_STD_VER >= 11
82{
83typedef std::multiset<int, std::less<int>, min_allocator<int>> M;
84typedef int V;
85typedef M::iterator I;
86V ar[] =
87{
881,
892,
903,
914,
925,
936,
947,
958
96};
97M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
98assert(m.size() == 8);
99I i = m.erase(std::next(m.cbegin(), 5), std::next(m.cbegin(), 5));
100assert(m.size() == 8);
101assert(i == std::next(m.begin(), 5));
102assert(*std::next(m.begin(), 0) == 1);
103assert(*std::next(m.begin(), 1) == 2);
104assert(*std::next(m.begin(), 2) == 3);
105assert(*std::next(m.begin(), 3) == 4);
106assert(*std::next(m.begin(), 4) == 5);
107assert(*std::next(m.begin(), 5) == 6);
108assert(*std::next(m.begin(), 6) == 7);
109assert(*std::next(m.begin(), 7) == 8);
110
111i = m.erase(std::next(m.cbegin(), 3), std::next(m.cbegin(), 4));
112assert(m.size() == 7);
113assert(i == std::next(m.begin(), 3));
114assert(*std::next(m.begin(), 0) == 1);
115assert(*std::next(m.begin(), 1) == 2);
116assert(*std::next(m.begin(), 2) == 3);
117assert(*std::next(m.begin(), 3) == 5);
118assert(*std::next(m.begin(), 4) == 6);
119assert(*std::next(m.begin(), 5) == 7);
120assert(*std::next(m.begin(), 6) == 8);
121
122i = m.erase(std::next(m.cbegin(), 2), std::next(m.cbegin(), 5));
123assert(m.size() == 4);
124assert(i == std::next(m.begin(), 2));
125assert(*std::next(m.begin(), 0) == 1);
126assert(*std::next(m.begin(), 1) == 2);
127assert(*std::next(m.begin(), 2) == 7);
128assert(*std::next(m.begin(), 3) == 8);
129
130i = m.erase(std::next(m.cbegin(), 0), std::next(m.cbegin(), 2));
131assert(m.size() == 2);
132assert(i == std::next(m.begin(), 0));
133assert(*std::next(m.begin(), 0) == 7);
134assert(*std::next(m.begin(), 1) == 8);
135
136i = m.erase(m.cbegin(), m.cend());
137assert(m.size() == 0);
138assert(i == m.end());
139}
140#endif
141
142return 0;
143}
144