libstdc++
stl_queue.h
Go to the documentation of this file.
1// Queue implementation -*- C++ -*-
2
3// Copyright (C) 2001-2026 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
51/** @file bits/stl_queue.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{queue}
54 */
55
56#ifndef _STL_QUEUE_H
57#define _STL_QUEUE_H 1
58
59#include <bits/concept_check.h>
60#include <debug/debug.h>
61#if __cplusplus >= 201103L
62# include <bits/uses_allocator.h>
63#endif
64#if __glibcxx_containers_ranges // C++ >= 23
65# include <ranges> // ranges::to
66# include <bits/ranges_algobase.h> // ranges::copy
67#endif
68
69namespace std _GLIBCXX_VISIBILITY(default)
70{
71_GLIBCXX_BEGIN_NAMESPACE_VERSION
72
73#if __glibcxx_format_ranges
74 template<typename, typename> class formatter;
75#endif
76
77 /**
78 * @brief A standard container giving FIFO behavior.
79 *
80 * @ingroup sequences
81 *
82 * @tparam _Tp Type of element.
83 * @tparam _Sequence Type of underlying sequence, defaults to deque<_Tp>.
84 *
85 * Meets many of the requirements of a
86 * <a href="tables.html#65">container</a>,
87 * but does not define anything to do with iterators. Very few of the
88 * other standard container interfaces are defined.
89 *
90 * This is not a true container, but an @e adaptor. It holds another
91 * container, and provides a wrapper interface to that container. The
92 * wrapper is what enforces strict first-in-first-out %queue behavior.
93 *
94 * The second template parameter defines the type of the underlying
95 * sequence/container. It defaults to std::deque, but it can be any type
96 * that supports @c front, @c back, @c push_back, and @c pop_front,
97 * such as std::list or an appropriate user-defined type.
98 *
99 * Members not found in @a normal containers are @c container_type,
100 * which is a typedef for the second Sequence parameter, and @c push and
101 * @c pop, which are standard %queue/FIFO operations.
102 */
103 template<typename _Tp, typename _Sequence = deque<_Tp> >
104 class queue
105 {
106#ifdef _GLIBCXX_CONCEPT_CHECKS
107 // concept requirements
108 typedef typename _Sequence::value_type _Sequence_value_type;
109# if __cplusplus < 201103L
110 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
111# endif
112 __glibcxx_class_requires(_Sequence, _FrontInsertionSequenceConcept)
113 __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
114 __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
115#endif
116
117 template<typename _Tp1, typename _Seq1>
118 friend _GLIBCXX26_CONSTEXPR bool
119 operator==(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
120
121 template<typename _Tp1, typename _Seq1>
122 friend _GLIBCXX26_CONSTEXPR bool
123 operator<(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
124
125#if __cpp_lib_three_way_comparison
126 template<typename _Tp1, three_way_comparable _Seq1>
127 friend _GLIBCXX26_CONSTEXPR compare_three_way_result_t<_Seq1>
128 operator<=>(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
129#endif
130
131#if __cplusplus >= 201103L
132 template<typename _Alloc>
133 using _Uses = typename
135
136#if __cplusplus >= 201703L
137 // _GLIBCXX_RESOLVE_LIB_DEFECTS
138 // 2566. Requirements on the first template parameter of container
139 // adaptors
141 "value_type must be the same as the underlying container");
142#endif // C++17
143#endif // C++11
144
145 public:
146 typedef typename _Sequence::value_type value_type;
147 typedef typename _Sequence::reference reference;
148 typedef typename _Sequence::const_reference const_reference;
149 typedef typename _Sequence::size_type size_type;
150 typedef _Sequence container_type;
151
152 protected:
153 /* Maintainers wondering why this isn't uglified as per style
154 * guidelines should note that this name is specified in the standard,
155 * C++98 [23.2.3.1].
156 * (Why? Presumably for the same reason that it's protected instead
157 * of private: to allow derivation. But none of the other
158 * containers allow for derivation. Odd.)
159 */
160 /// @c c is the underlying container.
161 _Sequence c;
162
163 public:
164 /**
165 * @brief Default constructor creates no elements.
166 */
167#if __cplusplus < 201103L
168 explicit
169 queue(const _Sequence& __c = _Sequence())
170 : c(__c) { }
171#else
172 template<typename _Seq = _Sequence, typename _Requires = typename
174 _GLIBCXX26_CONSTEXPR
176 : c() { }
177
178 explicit _GLIBCXX26_CONSTEXPR
179 queue(const _Sequence& __c)
180 : c(__c) { }
181
182 explicit _GLIBCXX26_CONSTEXPR
183 queue(_Sequence&& __c)
184 : c(std::move(__c)) { }
185
186 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
187 explicit _GLIBCXX26_CONSTEXPR
188 queue(const _Alloc& __a)
189 : c(__a) { }
190
191 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
192 _GLIBCXX26_CONSTEXPR
193 queue(const _Sequence& __c, const _Alloc& __a)
194 : c(__c, __a) { }
195
196 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
197 _GLIBCXX26_CONSTEXPR
198 queue(_Sequence&& __c, const _Alloc& __a)
199 : c(std::move(__c), __a) { }
200
201 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
202 _GLIBCXX26_CONSTEXPR
203 queue(const queue& __q, const _Alloc& __a)
204 : c(__q.c, __a) { }
205
206 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
207 _GLIBCXX26_CONSTEXPR
208 queue(queue&& __q, const _Alloc& __a)
209 : c(std::move(__q.c), __a) { }
210#endif
211
212#ifdef __glibcxx_adaptor_iterator_pair_constructor // C++ >= 23 && HOSTED
213 template<typename _InputIterator,
214 typename = _RequireInputIter<_InputIterator>>
215 _GLIBCXX26_CONSTEXPR
216 queue(_InputIterator __first, _InputIterator __last)
217 : c(__first, __last) { }
218
219 template<typename _InputIterator, typename _Alloc,
220 typename = _RequireInputIter<_InputIterator>,
221 typename = _Uses<_Alloc>>
222 _GLIBCXX26_CONSTEXPR
223 queue(_InputIterator __first, _InputIterator __last, const _Alloc& __a)
224 : c(__first, __last, __a) { }
225#endif
226
227#if __glibcxx_containers_ranges // C++ >= 23
228 /**
229 * @brief Construct a queue from a range.
230 * @since C++23
231 */
232 template<__detail::__container_compatible_range<_Tp> _Rg>
233 _GLIBCXX26_CONSTEXPR
234 queue(from_range_t, _Rg&& __rg)
235 : c(ranges::to<_Sequence>(std::forward<_Rg>(__rg)))
236 { }
237
238 /**
239 * @brief Construct a queue from a range.
240 * @since C++23
241 */
242 template<__detail::__container_compatible_range<_Tp> _Rg,
243 typename _Alloc>
244 _GLIBCXX26_CONSTEXPR
245 queue(from_range_t, _Rg&& __rg, const _Alloc& __a)
246 : c(ranges::to<_Sequence>(std::forward<_Rg>(__rg), __a))
247 { }
248#endif
249
250 /**
251 * Returns true if the %queue is empty.
252 */
253 _GLIBCXX_NODISCARD
254 _GLIBCXX26_CONSTEXPR bool
255 empty() const
256 { return c.empty(); }
257
258 /** Returns the number of elements in the %queue. */
259 _GLIBCXX_NODISCARD
260 _GLIBCXX26_CONSTEXPR size_type
261 size() const
262 { return c.size(); }
263
264 /**
265 * Returns a read/write reference to the data at the first
266 * element of the %queue.
267 */
268 _GLIBCXX_NODISCARD
269 _GLIBCXX26_CONSTEXPR reference
271 {
272 __glibcxx_requires_nonempty();
273 return c.front();
274 }
275
276 /**
277 * Returns a read-only (constant) reference to the data at the first
278 * element of the %queue.
279 */
280 _GLIBCXX_NODISCARD
281 _GLIBCXX26_CONSTEXPR const_reference
282 front() const
283 {
284 __glibcxx_requires_nonempty();
285 return c.front();
286 }
287
288 /**
289 * Returns a read/write reference to the data at the last
290 * element of the %queue.
291 */
292 _GLIBCXX_NODISCARD
293 _GLIBCXX26_CONSTEXPR reference
295 {
296 __glibcxx_requires_nonempty();
297 return c.back();
298 }
299
300 /**
301 * Returns a read-only (constant) reference to the data at the last
302 * element of the %queue.
303 */
304 _GLIBCXX_NODISCARD
305 _GLIBCXX26_CONSTEXPR const_reference
306 back() const
307 {
308 __glibcxx_requires_nonempty();
309 return c.back();
310 }
311
312 /**
313 * @brief Add data to the end of the %queue.
314 * @param __x Data to be added.
315 *
316 * This is a typical %queue operation. The function creates an
317 * element at the end of the %queue and assigns the given data
318 * to it. The time complexity of the operation depends on the
319 * underlying sequence.
320 */
321 _GLIBCXX26_CONSTEXPR void
322 push(const value_type& __x)
323 { c.push_back(__x); }
324
325#if __cplusplus >= 201103L
326 _GLIBCXX26_CONSTEXPR void
327 push(value_type&& __x)
328 { c.push_back(std::move(__x)); }
329
330#if __cplusplus > 201402L
331 template<typename... _Args>
332 _GLIBCXX26_CONSTEXPR decltype(auto)
333 emplace(_Args&&... __args)
334 { return c.emplace_back(std::forward<_Args>(__args)...); }
335#else
336 template<typename... _Args>
337 void
338 emplace(_Args&&... __args)
339 { c.emplace_back(std::forward<_Args>(__args)...); }
340#endif
341#endif
342
343#if __glibcxx_containers_ranges // C++ >= 23
344 template<__detail::__container_compatible_range<_Tp> _Rg>
345 _GLIBCXX26_CONSTEXPR void
346 push_range(_Rg&& __rg)
347 {
348 if constexpr (requires { c.append_range(std::forward<_Rg>(__rg)); })
349 c.append_range(std::forward<_Rg>(__rg));
350 else
351 ranges::copy(__rg, std::back_inserter(c));
352 }
353#endif
354
355 /**
356 * @brief Removes first element.
357 *
358 * This is a typical %queue operation. It shrinks the %queue by one.
359 * The time complexity of the operation depends on the underlying
360 * sequence.
361 *
362 * Note that no data is returned, and if the first element's
363 * data is needed, it should be retrieved before pop() is
364 * called.
365 */
366 _GLIBCXX26_CONSTEXPR void
368 {
369 __glibcxx_requires_nonempty();
370 c.pop_front();
371 }
372
373#if __cplusplus >= 201103L
374 _GLIBCXX26_CONSTEXPR void
375 swap(queue& __q)
376#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
377 noexcept(__is_nothrow_swappable<_Sequence>::value)
378#else
379 noexcept(__is_nothrow_swappable<_Tp>::value)
380#endif
381 {
382 using std::swap;
383 swap(c, __q.c);
384 }
385#endif // __cplusplus >= 201103L
386
387#if __glibcxx_format_ranges
388 friend class formatter<queue<_Tp, _Sequence>, char>;
389 friend class formatter<queue<_Tp, _Sequence>, wchar_t>;
390#endif
391 };
392
393#if __cpp_deduction_guides >= 201606
394 template<typename _Container,
395 typename = _RequireNotAllocator<_Container>>
397
398 template<typename _Container, typename _Allocator,
399 typename = _RequireNotAllocator<_Container>>
400 queue(_Container, _Allocator)
402
403#ifdef __glibcxx_adaptor_iterator_pair_constructor
404 template<typename _InputIterator,
405 typename _ValT
407 typename = _RequireInputIter<_InputIterator>>
408 queue(_InputIterator, _InputIterator) -> queue<_ValT>;
409
410 template<typename _InputIterator, typename _Allocator,
411 typename _ValT
413 typename = _RequireInputIter<_InputIterator>,
414 typename = _RequireAllocator<_Allocator>>
415 queue(_InputIterator, _InputIterator, _Allocator)
417#endif
418
419#if __glibcxx_containers_ranges // C++ >= 23
420 template<ranges::input_range _Rg>
421 queue(from_range_t, _Rg&&) -> queue<ranges::range_value_t<_Rg>>;
422
423 template<ranges::input_range _Rg, __allocator_like _Alloc>
424 queue(from_range_t, _Rg&&, _Alloc)
427#endif
428#endif
429
430 /**
431 * @brief Queue equality comparison.
432 * @param __x A %queue.
433 * @param __y A %queue of the same type as @a __x.
434 * @return True iff the size and elements of the queues are equal.
435 *
436 * This is an equivalence relation. Complexity and semantics depend on the
437 * underlying sequence type, but the expected rules are: this relation is
438 * linear in the size of the sequences, and queues are considered equivalent
439 * if their sequences compare equal.
440 */
441 template<typename _Tp, typename _Seq>
442 _GLIBCXX_NODISCARD
443 inline _GLIBCXX26_CONSTEXPR bool
444 operator==(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
445 { return __x.c == __y.c; }
446
447 /**
448 * @brief Queue ordering relation.
449 * @param __x A %queue.
450 * @param __y A %queue of the same type as @a x.
451 * @return True iff @a __x is lexicographically less than @a __y.
452 *
453 * This is an total ordering relation. Complexity and semantics
454 * depend on the underlying sequence type, but the expected rules
455 * are: this relation is linear in the size of the sequences, the
456 * elements must be comparable with @c <, and
457 * std::lexicographical_compare() is usually used to make the
458 * determination.
459 */
460 template<typename _Tp, typename _Seq>
461 _GLIBCXX_NODISCARD
462 inline _GLIBCXX26_CONSTEXPR bool
463 operator<(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
464 { return __x.c < __y.c; }
465
466 /// Based on operator==
467 template<typename _Tp, typename _Seq>
468 _GLIBCXX_NODISCARD
469 inline _GLIBCXX26_CONSTEXPR bool
470 operator!=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
471 { return !(__x == __y); }
472
473 /// Based on operator<
474 template<typename _Tp, typename _Seq>
475 _GLIBCXX_NODISCARD
476 inline _GLIBCXX26_CONSTEXPR bool
477 operator>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
478 { return __y < __x; }
479
480 /// Based on operator<
481 template<typename _Tp, typename _Seq>
482 _GLIBCXX_NODISCARD
483 inline _GLIBCXX26_CONSTEXPR bool
484 operator<=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
485 { return !(__y < __x); }
486
487 /// Based on operator<
488 template<typename _Tp, typename _Seq>
489 _GLIBCXX_NODISCARD
490 inline _GLIBCXX26_CONSTEXPR bool
491 operator>=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
492 { return !(__x < __y); }
493
494#if __cpp_lib_three_way_comparison
495 template<typename _Tp, three_way_comparable _Seq>
496 [[nodiscard]]
497 inline _GLIBCXX26_CONSTEXPR compare_three_way_result_t<_Seq>
498 operator<=>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
499 { return __x.c <=> __y.c; }
500#endif
501
502#if __cplusplus >= 201103L
503 template<typename _Tp, typename _Seq>
504 inline _GLIBCXX26_CONSTEXPR
505#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
506 // Constrained free swap overload, see p0185r1
508#else
509 void
510#endif
511 swap(queue<_Tp, _Seq>& __x, queue<_Tp, _Seq>& __y)
512 noexcept(noexcept(__x.swap(__y)))
513 { __x.swap(__y); }
514
515 template<typename _Tp, typename _Seq, typename _Alloc>
516 struct uses_allocator<queue<_Tp, _Seq>, _Alloc>
517 : public uses_allocator<_Seq, _Alloc>::type { };
518#endif // __cplusplus >= 201103L
519
520 /**
521 * @brief A standard container automatically sorting its contents.
522 *
523 * @ingroup sequences
524 *
525 * @tparam _Tp Type of element.
526 * @tparam _Sequence Type of underlying sequence, defaults to vector<_Tp>.
527 * @tparam _Compare Comparison function object type, defaults to
528 * less<_Sequence::value_type>.
529 *
530 * This is not a true container, but an @e adaptor. It holds
531 * another container, and provides a wrapper interface to that
532 * container. The wrapper is what enforces priority-based sorting
533 * and %queue behavior. Very few of the standard container/sequence
534 * interface requirements are met (e.g., iterators).
535 *
536 * The second template parameter defines the type of the underlying
537 * sequence/container. It defaults to std::vector, but it can be
538 * any type that supports @c front(), @c push_back, @c pop_back,
539 * and random-access iterators, such as std::deque or an
540 * appropriate user-defined type.
541 *
542 * The third template parameter supplies the means of making
543 * priority comparisons. It defaults to @c less<value_type> but
544 * can be anything defining a strict weak ordering.
545 *
546 * Members not found in @a normal containers are @c container_type,
547 * which is a typedef for the second Sequence parameter, and @c
548 * push, @c pop, and @c top, which are standard %queue operations.
549 *
550 * @note No equality/comparison operators are provided for
551 * %priority_queue.
552 *
553 * @note Sorting of the elements takes place as they are added to,
554 * and removed from, the %priority_queue using the
555 * %priority_queue's member functions. If you access the elements
556 * by other means, and change their data such that the sorting
557 * order would be different, the %priority_queue will not re-sort
558 * the elements for you. (How could it know to do so?)
559 */
560 template<typename _Tp, typename _Sequence = vector<_Tp>,
561 typename _Compare = less<typename _Sequence::value_type> >
563 {
564#ifdef _GLIBCXX_CONCEPT_CHECKS
565 // concept requirements
566 typedef typename _Sequence::value_type _Sequence_value_type;
567# if __cplusplus < 201103L
568 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
569# endif
570 __glibcxx_class_requires(_Sequence, _SequenceConcept)
571 __glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept)
572 __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
573 __glibcxx_class_requires4(_Compare, bool, _Tp, _Tp,
574 _BinaryFunctionConcept)
575#endif
576
577#if __cplusplus >= 201103L
578 template<typename _Alloc>
579 using _Uses = typename
581
582#if __cplusplus >= 201703L
583 // _GLIBCXX_RESOLVE_LIB_DEFECTS
584 // 2566. Requirements on the first template parameter of container
585 // adaptors
587 "value_type must be the same as the underlying container");
588#endif // C++17
589#endif // C++11
590
591 public:
592 typedef typename _Sequence::value_type value_type;
593 typedef typename _Sequence::reference reference;
594 typedef typename _Sequence::const_reference const_reference;
595 typedef typename _Sequence::size_type size_type;
596 typedef _Sequence container_type;
597 // _GLIBCXX_RESOLVE_LIB_DEFECTS
598 // DR 2684. priority_queue lacking comparator typedef
599 typedef _Compare value_compare;
600
601 protected:
602 // See queue::c for notes on these names.
603 _Sequence c;
604 _Compare comp;
605
606 public:
607 /**
608 * @brief Default constructor creates no elements.
609 */
610#if __cplusplus < 201103L
611 explicit
612 priority_queue(const _Compare& __x = _Compare(),
613 const _Sequence& __s = _Sequence())
614 : c(__s), comp(__x)
615 { std::make_heap(c.begin(), c.end(), comp); }
616#else
617 template<typename _Seq = _Sequence, typename _Requires = typename
619 is_default_constructible<_Seq>>::value>::type>
620 _GLIBCXX26_CONSTEXPR
622 : c(), comp() { }
623
624 explicit _GLIBCXX26_CONSTEXPR
625 priority_queue(const _Compare& __x, const _Sequence& __s)
626 : c(__s), comp(__x)
627 { std::make_heap(c.begin(), c.end(), comp); }
628
629 explicit _GLIBCXX26_CONSTEXPR
630 priority_queue(const _Compare& __x, _Sequence&& __s = _Sequence())
631 : c(std::move(__s)), comp(__x)
632 { std::make_heap(c.begin(), c.end(), comp); }
633
634 priority_queue(const priority_queue&) = default;
635 priority_queue& operator=(const priority_queue&) = default;
636
637 _GLIBCXX26_CONSTEXPR
639 noexcept(__and_<is_nothrow_move_constructible<_Sequence>,
640 is_nothrow_move_constructible<_Compare>>::value)
641 : c(std::move(__q.c)), comp(std::move(__q.comp))
642 { __q.c.clear(); }
643
644 _GLIBCXX26_CONSTEXPR priority_queue&
645 operator=(priority_queue&& __q)
646 noexcept(__and_<is_nothrow_move_assignable<_Sequence>,
647 is_nothrow_move_assignable<_Compare>>::value)
648 {
649 c = std::move(__q.c);
650 __q.c.clear();
651 comp = std::move(__q.comp);
652 return *this;
653 }
654
655 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
656 explicit _GLIBCXX26_CONSTEXPR
657 priority_queue(const _Alloc& __a)
658 : c(__a), comp() { }
659
660 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
661 _GLIBCXX26_CONSTEXPR
662 priority_queue(const _Compare& __x, const _Alloc& __a)
663 : c(__a), comp(__x) { }
664
665 // _GLIBCXX_RESOLVE_LIB_DEFECTS
666 // 2537. Constructors [...] taking allocators should call make_heap
667 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
668 _GLIBCXX26_CONSTEXPR
669 priority_queue(const _Compare& __x, const _Sequence& __c,
670 const _Alloc& __a)
671 : c(__c, __a), comp(__x)
672 { std::make_heap(c.begin(), c.end(), comp); }
673
674 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
675 _GLIBCXX26_CONSTEXPR
676 priority_queue(const _Compare& __x, _Sequence&& __c, const _Alloc& __a)
677 : c(std::move(__c), __a), comp(__x)
678 { std::make_heap(c.begin(), c.end(), comp); }
679
680 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
681 _GLIBCXX26_CONSTEXPR
682 priority_queue(const priority_queue& __q, const _Alloc& __a)
683 : c(__q.c, __a), comp(__q.comp) { }
684
685 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
686 _GLIBCXX26_CONSTEXPR
687 priority_queue(priority_queue&& __q, const _Alloc& __a)
688 : c(std::move(__q.c), __a), comp(std::move(__q.comp))
689 { __q.c.clear(); }
690#endif
691
692 /**
693 * @brief Builds a %queue from a range.
694 * @param __first An input iterator.
695 * @param __last An input iterator.
696 * @param __x A comparison functor describing a strict weak ordering.
697 * @param __s An initial sequence with which to start.
698 *
699 * Begins by copying @a __s, inserting a copy of the elements
700 * from @a [first,last) into the copy of @a __s, then ordering
701 * the copy according to @a __x.
702 *
703 * For more information on function objects, see the
704 * documentation on @link functors functor base classes@endlink.
705 *
706 * @{
707 */
708#if __cplusplus < 201103L
709 template<typename _InputIterator>
710 priority_queue(_InputIterator __first, _InputIterator __last,
711 const _Compare& __x = _Compare(),
712 const _Sequence& __s = _Sequence())
713 : c(__s), comp(__x)
714 {
715 __glibcxx_requires_valid_range(__first, __last);
716 c.insert(c.end(), __first, __last);
717 std::make_heap(c.begin(), c.end(), comp);
718 }
719#else
720 // _GLIBCXX_RESOLVE_LIB_DEFECTS
721 // 3529. priority_queue(first, last) should construct c with (first, last)
722 template<typename _InputIterator,
723 typename = std::_RequireInputIter<_InputIterator>>
724 _GLIBCXX26_CONSTEXPR
725 priority_queue(_InputIterator __first, _InputIterator __last,
726 const _Compare& __x = _Compare())
727 : c(__first, __last), comp(__x)
728 { std::make_heap(c.begin(), c.end(), comp); }
729
730 // _GLIBCXX_RESOLVE_LIB_DEFECTS
731 // 3522. Missing requirement on InputIterator template parameter
732 template<typename _InputIterator,
733 typename = std::_RequireInputIter<_InputIterator>>
734 _GLIBCXX26_CONSTEXPR
735 priority_queue(_InputIterator __first, _InputIterator __last,
736 const _Compare& __x, const _Sequence& __s)
737 : c(__s), comp(__x)
738 {
739 __glibcxx_requires_valid_range(__first, __last);
740 c.insert(c.end(), __first, __last);
741 std::make_heap(c.begin(), c.end(), comp);
742 }
743
744 template<typename _InputIterator,
745 typename = std::_RequireInputIter<_InputIterator>>
746 _GLIBCXX26_CONSTEXPR
747 priority_queue(_InputIterator __first, _InputIterator __last,
748 const _Compare& __x, _Sequence&& __s)
749 : c(std::move(__s)), comp(__x)
750 {
751 __glibcxx_requires_valid_range(__first, __last);
752 c.insert(c.end(), __first, __last);
753 std::make_heap(c.begin(), c.end(), comp);
754 }
755#endif // C++11
756 /// @}
757
758#if __cplusplus >= 201103L
759 // _GLIBCXX_RESOLVE_LIB_DEFECTS
760 // 3506. Missing allocator-extended constructors for priority_queue
761 template<typename _InputIterator, typename _Alloc,
762 typename = std::_RequireInputIter<_InputIterator>,
763 typename _Requires = _Uses<_Alloc>>
764 _GLIBCXX26_CONSTEXPR
765 priority_queue(_InputIterator __first, _InputIterator __last,
766 const _Alloc& __alloc)
767 : c(__first, __last, __alloc), comp()
768 { std::make_heap(c.begin(), c.end(), comp); }
769
770 template<typename _InputIterator, typename _Alloc,
771 typename = std::_RequireInputIter<_InputIterator>,
772 typename _Requires = _Uses<_Alloc>>
773 _GLIBCXX26_CONSTEXPR
774 priority_queue(_InputIterator __first, _InputIterator __last,
775 const _Compare& __x, const _Alloc& __alloc)
776 : c(__first, __last, __alloc), comp(__x)
777 { std::make_heap(c.begin(), c.end(), comp); }
778
779 template<typename _InputIterator, typename _Alloc,
780 typename = std::_RequireInputIter<_InputIterator>,
781 typename _Requires = _Uses<_Alloc>>
782 _GLIBCXX26_CONSTEXPR
783 priority_queue(_InputIterator __first, _InputIterator __last,
784 const _Compare& __x, const _Sequence& __s,
785 const _Alloc& __alloc)
786 : c(__s, __alloc), comp(__x)
787 {
788 __glibcxx_requires_valid_range(__first, __last);
789 c.insert(c.end(), __first, __last);
790 std::make_heap(c.begin(), c.end(), comp);
791 }
792
793 template<typename _InputIterator, typename _Alloc,
794 typename _Requires = _Uses<_Alloc>>
795 _GLIBCXX26_CONSTEXPR
796 priority_queue(_InputIterator __first, _InputIterator __last,
797 const _Compare& __x, _Sequence&& __s,
798 const _Alloc& __alloc)
799 : c(std::move(__s), __alloc), comp(__x)
800 {
801 __glibcxx_requires_valid_range(__first, __last);
802 c.insert(c.end(), __first, __last);
803 std::make_heap(c.begin(), c.end(), comp);
804 }
805#endif // C++11
806
807#if __glibcxx_containers_ranges // C++ >= 23
808 /**
809 * @brief Construct a priority_queue from a range.
810 * @since C++23
811 *
812 * @{
813 */
814 template<__detail::__container_compatible_range<_Tp> _Rg>
815 _GLIBCXX26_CONSTEXPR
816 priority_queue(from_range_t, _Rg&& __rg,
817 const _Compare& __x = _Compare())
818 : c(ranges::to<_Sequence>(std::forward<_Rg>(__rg))), comp(__x)
819 { std::make_heap(c.begin(), c.end(), comp); }
820
821 template<__detail::__container_compatible_range<_Tp> _Rg, typename _Alloc>
822 _GLIBCXX26_CONSTEXPR
823 priority_queue(from_range_t, _Rg&& __rg, const _Compare& __x,
824 const _Alloc& __a)
825 : c(ranges::to<_Sequence>(std::forward<_Rg>(__rg), __a)), comp(__x)
826 { std::make_heap(c.begin(), c.end(), comp); }
827
828 template<__detail::__container_compatible_range<_Tp> _Rg, typename _Alloc>
829 _GLIBCXX26_CONSTEXPR
830 priority_queue(from_range_t, _Rg&& __rg, const _Alloc& __a)
831 : c(ranges::to<_Sequence>(std::forward<_Rg>(__rg), __a)), comp()
832 { std::make_heap(c.begin(), c.end(), comp); }
833 /// @}
834#endif
835
836 /**
837 * Returns true if the %queue is empty.
838 */
839 _GLIBCXX_NODISCARD
840 _GLIBCXX26_CONSTEXPR bool
841 empty() const
842 { return c.empty(); }
843
844 /** Returns the number of elements in the %queue. */
845 _GLIBCXX_NODISCARD
846 _GLIBCXX26_CONSTEXPR size_type
847 size() const
848 { return c.size(); }
849
850 /**
851 * Returns a read-only (constant) reference to the data at the first
852 * element of the %queue.
853 */
854 _GLIBCXX_NODISCARD
855 _GLIBCXX26_CONSTEXPR const_reference
856 top() const
857 {
858 __glibcxx_requires_nonempty();
859 return c.front();
860 }
861
862 /**
863 * @brief Add data to the %queue.
864 * @param __x Data to be added.
865 *
866 * This is a typical %queue operation.
867 * The time complexity of the operation depends on the underlying
868 * sequence.
869 */
870 _GLIBCXX26_CONSTEXPR void
871 push(const value_type& __x)
872 {
873 c.push_back(__x);
874 std::push_heap(c.begin(), c.end(), comp);
875 }
876
877#if __cplusplus >= 201103L
878 _GLIBCXX26_CONSTEXPR void
879 push(value_type&& __x)
880 {
881 c.push_back(std::move(__x));
882 std::push_heap(c.begin(), c.end(), comp);
883 }
884
885 template<typename... _Args>
886 _GLIBCXX26_CONSTEXPR void
887 emplace(_Args&&... __args)
888 {
889 c.emplace_back(std::forward<_Args>(__args)...);
890 std::push_heap(c.begin(), c.end(), comp);
891 }
892#endif
893
894#if __glibcxx_containers_ranges // C++ >= 23
895 template<__detail::__container_compatible_range<_Tp> _Rg>
896 _GLIBCXX26_CONSTEXPR void
897 push_range(_Rg&& __rg)
898 {
899 if constexpr (requires { c.append_range(std::forward<_Rg>(__rg)); })
900 c.append_range(std::forward<_Rg>(__rg));
901 else
902 ranges::copy(__rg, std::back_inserter(c));
903 std::make_heap(c.begin(), c.end(), comp);
904 }
905#endif
906
907 /**
908 * @brief Removes first element.
909 *
910 * This is a typical %queue operation. It shrinks the %queue
911 * by one. The time complexity of the operation depends on the
912 * underlying sequence.
913 *
914 * Note that no data is returned, and if the first element's
915 * data is needed, it should be retrieved before pop() is
916 * called.
917 */
918 _GLIBCXX26_CONSTEXPR void
920 {
921 __glibcxx_requires_nonempty();
922 std::pop_heap(c.begin(), c.end(), comp);
923 c.pop_back();
924 }
925
926#if __cplusplus >= 201103L
927 _GLIBCXX26_CONSTEXPR void
928 swap(priority_queue& __pq)
929 noexcept(__and_<
930#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
931 __is_nothrow_swappable<_Sequence>,
932#else
933 __is_nothrow_swappable<_Tp>,
934#endif
935 __is_nothrow_swappable<_Compare>
936 >::value)
937 {
938 using std::swap;
939 swap(c, __pq.c);
940 swap(comp, __pq.comp);
941 }
942#endif // __cplusplus >= 201103L
943
944#if __glibcxx_format_ranges
945 friend class formatter<priority_queue<_Tp, _Sequence, _Compare>, char>;
946 friend class formatter<priority_queue<_Tp, _Sequence, _Compare>, wchar_t>;
947#endif
948 };
949
950#if __cpp_deduction_guides >= 201606
951 template<typename _Compare, typename _Container,
952 typename = _RequireNotAllocator<_Compare>,
953 typename = _RequireNotAllocator<_Container>>
954 priority_queue(_Compare, _Container)
956
957 template<typename _InputIterator, typename _ValT
959 typename _Compare = less<_ValT>,
960 typename _Container = vector<_ValT>,
961 typename = _RequireInputIter<_InputIterator>,
962 typename = _RequireNotAllocator<_Compare>,
963 typename = _RequireNotAllocator<_Container>>
964 priority_queue(_InputIterator, _InputIterator, _Compare = _Compare(),
965 _Container = _Container())
967
968 template<typename _Compare, typename _Container, typename _Allocator,
969 typename = _RequireNotAllocator<_Compare>,
970 typename = _RequireNotAllocator<_Container>>
971 priority_queue(_Compare, _Container, _Allocator)
973
974#if __glibcxx_containers_ranges // C++ >= 23
975 template<ranges::input_range _Rg,
976 __not_allocator_like _Compare = less<ranges::range_value_t<_Rg>>,
977 __allocator_like _Alloc = std::allocator<ranges::range_value_t<_Rg>>>
978 priority_queue(from_range_t, _Rg&&, _Compare = _Compare(),
979 _Alloc = _Alloc())
982 _Compare>;
983
984 template<ranges::input_range _Rg, __allocator_like _Alloc>
985 priority_queue(from_range_t, _Rg&&, _Alloc)
988#endif
989#endif
990
991 // No equality/comparison operators are provided for priority_queue.
992
993#if __cplusplus >= 201103L
994 template<typename _Tp, typename _Sequence, typename _Compare>
995 inline _GLIBCXX26_CONSTEXPR
996#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
997 // Constrained free swap overload, see p0185r1
999 __is_swappable<_Compare>>::value>::type
1000#else
1001 void
1002#endif
1005 noexcept(noexcept(__x.swap(__y)))
1006 { __x.swap(__y); }
1007
1008 template<typename _Tp, typename _Sequence, typename _Compare,
1009 typename _Alloc>
1010 struct uses_allocator<priority_queue<_Tp, _Sequence, _Compare>, _Alloc>
1011 : public uses_allocator<_Sequence, _Alloc>::type { };
1012#endif // __cplusplus >= 201103L
1013
1014_GLIBCXX_END_NAMESPACE_VERSION
1015} // namespace
1016
1017#endif /* _STL_QUEUE_H */
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
constexpr back_insert_iterator< _Container > back_inserter(_Container &__x)
ISO C++ entities toplevel namespace is std.
typename __detail::__cmp3way_res_impl< _Tp, _Up >::type compare_three_way_result_t
[cmp.result], result of three-way comparison
Definition compare:547
Define a member typedef type only if a boolean constant is true.
Definition type_traits:137
is_default_constructible
Definition type_traits:1274
Declare uses_allocator so it can be specialized in <queue> etc.
Definition memoryfwd.h:76
A standard container using fixed-size memory allocation and constant-time manipulation of elements at...
Definition stl_deque.h:854
One of the comparison functors.
A standard container giving FIFO behavior.
Definition stl_queue.h:105
constexpr queue()
Default constructor creates no elements.
Definition stl_queue.h:175
constexpr void push(const value_type &__x)
Add data to the end of the queue.
Definition stl_queue.h:322
_Sequence c
c is the underlying container.
Definition stl_queue.h:161
constexpr reference back()
Definition stl_queue.h:294
constexpr const_reference front() const
Definition stl_queue.h:282
constexpr reference front()
Definition stl_queue.h:270
constexpr size_type size() const
Definition stl_queue.h:261
constexpr void pop()
Removes first element.
Definition stl_queue.h:367
constexpr const_reference back() const
Definition stl_queue.h:306
constexpr bool empty() const
Definition stl_queue.h:255
A standard container automatically sorting its contents.
Definition stl_queue.h:563
constexpr void push(const value_type &__x)
Add data to the queue.
Definition stl_queue.h:871
constexpr void pop()
Removes first element.
Definition stl_queue.h:919
constexpr priority_queue()
Default constructor creates no elements.
Definition stl_queue.h:621
constexpr priority_queue(_InputIterator __first, _InputIterator __last, const _Compare &__x=_Compare())
Builds a queue from a range.
Definition stl_queue.h:725
constexpr priority_queue(_InputIterator __first, _InputIterator __last, const _Compare &__x, const _Sequence &__s)
Builds a queue from a range.
Definition stl_queue.h:735
constexpr bool empty() const
Definition stl_queue.h:841
constexpr priority_queue(_InputIterator __first, _InputIterator __last, const _Compare &__x, _Sequence &&__s)
Builds a queue from a range.
Definition stl_queue.h:747
constexpr size_type size() const
Definition stl_queue.h:847
constexpr const_reference top() const
Definition stl_queue.h:856
A standard container which offers fixed time access to individual elements in any order.
Definition stl_vector.h:511
A range for which ranges::begin returns an input iterator.