std::queue<T,Container>::queue - cppreference.com (2024)

[edit template]

C++

Compiler support
Freestanding and hosted
Language
Standard library
Standard library headers
Named requirements
Feature test macros (C++20)
Language support library
Concepts library (C++20)
Metaprogramming library (C++11)
Diagnostics library
General utilities library
Strings library
Containers library
Iterators library
Ranges library (C++20)
Algorithms library
Numerics library
Localizations library
Input/output library
Filesystem library (C++17)
Regular expressions library (C++11)
Concurrency support library (C++11)
Technical specifications
Symbols index
External libraries

[edit]

Containers library

Sequence

array

(C++11)

vector

vector<bool>

deque

forward_list

(C++11)

list

Associative

set

multiset

map

multimap

Unordered associative

unordered_set

(C++11)

unordered_multiset

(C++11)

unordered_map

(C++11)

unordered_multimap

(C++11)

Adaptors

stack

queue

priority_queue

flat_set

(C++23)

flat_multiset

(C++23)

flat_map

(C++23)

flat_multimap

(C++23)

Views

span

(C++20)

mdspan

(C++23)

Iterator invalidation
Member function table
Non-member function table

[edit]

std::queue

Member functions

queue::queue

queue::~queue

queue::operator=

Element access

queue::front

queue::back

Capacity

queue::empty

queue::size

Modifiers

queue::push

queue::push_range

(C++23)

queue::emplace

(C++11)

queue::pop

queue::swap

(C++11)

Non-member functions

swap(std::queue)

(C++11)

operator==operator!=operator<operator>operator<=operator>=operator<=>

(C++20)

Helper classes

uses_allocator<std::queue>

(C++11)

formatter<std::queue>

(C++23)

Deduction guides(C++17)

queue() : queue(Container()) {}

(1) (since C++11)
(2)

explicit queue( const Container& cont = Container() );

(until C++11)

explicit queue( const Container& cont );

(since C++11)

explicit queue( Container&& cont );

(3) (since C++11)

queue( const queue& other );

(4) (implicitly declared)

queue( queue&& other );

(5) (since C++11)
(implicitly declared)

template< class InputIt >
queue( InputIt first, InputIt last );

(6)(since C++23)

template< class Alloc >
explicit queue( const Alloc& alloc );

(7) (since C++11)

template< class Alloc >
queue( const Container& cont, const Alloc& alloc );

(8) (since C++11)

template< class Alloc >
queue( Container&& cont, const Alloc& alloc );

(9) (since C++11)

template< class Alloc >
queue( const queue& other, const Alloc& alloc );

(10) (since C++11)

template< class Alloc >
queue( queue&& other, const Alloc& alloc );

(11) (since C++11)

template< class InputIt, class Alloc >
queue( InputIt first, InputIt last, const Alloc& alloc );

(12)(since C++23)

template< container-compatible-range<T> R>
queue( std::from_range_t, R&& rg );

(13)(since C++23)

template< container-compatible-range<T> R, class Alloc >
queue( std::from_range_t, R&& rg, const Alloc& alloc );

(14)(since C++23)

Constructs new underlying container of the container adaptor from a variety of data sources.

1) Default constructor. Value-initializes the container.

2) Copy-constructs the underlying container c with the contents of cont. This is also the default constructor.(until C++11)

3) Move-constructs the underlying container c with std::move(cont).

4) Copy constructor. The adaptor is copy-constructed with the contents of other.c.

5) Move constructor. The adaptor is constructed with std::move(other.c).

6) Constructs the underlying container c with the contents of the range [first,last). This overload participates in overload resolution only if InputIt satisfies LegacyInputIterator.

7-12) These constructors participate in overload resolution only if std::uses_allocator<Container, Alloc>::value is true, that is, if the underlying container is an allocator-aware container (true for all standard library containers that can be used with queue).

7) Constructs the underlying container using alloc as allocator, as if by c(alloc).

8) Constructs the underlying container with the contents of cont and using alloc as allocator, as if by c(cont, alloc).

9) Constructs the underlying container with the contents of cont using move semantics while utilizing alloc as allocator, as if by c(std::move(cont), alloc).

10) Constructs the adaptor with the contents of other.c and using alloc as allocator, as if by c(other.c, alloc).

11) Constructs the adaptor with the contents of other using move semantics while utilizing alloc as allocator, as if by c(std::move(other.c), alloc).

12) Constructs the underlying container with the contents of the range [first,last) using alloc as allocator, as if by c(first, last, alloc). This overload participates in overload resolution only if InputIt satisfies LegacyInputIterator.

13) Constructs the underlying container with ranges::to<Container>(std::forward<R>(rg)).

14) Constructs the underlying container with ranges::to<Container>(std::forward<R>(rg), alloc).

Contents

  • 1 Parameters
  • 2 Complexity
  • 3 Notes
  • 4 Example
  • 5 Defect reports
  • 6 See also

[edit] Parameters

alloc - allocator to use for all memory allocations of the underlying container
other - another container adaptor to be used as source to initialize the underlying container
cont - container to be used as source to initialize the underlying container
first, last - range of elements [first,last) to initialize with
rg - a container compatible range, that is, an input_range whose elements are convertible to T
Type requirements
-Alloc must meet the requirements of Allocator.
-Container must meet the requirements of Container. The constructors taking an allocator parameter participate in overload resolution only if Container meets the requirements of AllocatorAwareContainer.
-InputIt must meet the requirements of LegacyInputIterator.

[edit] Complexity

Same as the corresponding operation on the wrapped container.

[edit] Notes

Feature-test macroValueStdFeature
__cpp_lib_adaptor_iterator_pair_constructor202106L(C++23)Iterator pair constructors for std::queue and std::stack; overloads (6) and (12)
__cpp_lib_containers_ranges202202L(C++23)Ranges-aware construction and insertion; overloads (13) and (14)

[edit] Example

Run this code

#include <cassert>#include <deque>#include <iostream>#include <memory>#include <ranges>#include <queue>int main(){ std::queue<int> c1; c1.push(5); assert(c1.size() == 1); std::queue<int> c2(c1); assert(c2.size() == 1); std::deque<int> deq{3, 1, 4, 1, 5}; std::queue<int> c3(deq); // overload (2) assert(c3.size() == 5);# ifdef __cpp_lib_adaptor_iterator_pair_constructor const auto il = {2, 7, 1, 8, 2}; std::queue<int> c4{il.begin(), il.end()}; // C++23, (6) assert(c4.size() == 5);# endif# if __cpp_lib_containers_ranges >= 202202L // C++23, overload (13) auto c5 = std::queue(std::from_range_t, std::ranges::iota(0, 42)); assert(c5.size() == 42); // the same effect with pipe syntax, internally uses overload (13) auto c6 = std::ranges::iota(0, 42) | std::ranges::to<std::queue>(); assert(c6.size() == 42); std::allocator<int> alloc; // C++23, overload (14) auto c7 = std::queue(std::from_range_t, std::ranges::iota(0, 42), alloc); assert(c7.size() == 42); // the same effect with pipe syntax, internally uses overload (14) auto c8 = std::ranges::iota(0, 42) | std::ranges::to<std::queue>(alloc); assert(c8.size() == 42);# endif}

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
P0935R0C++11default constructor was explicitmade implicit

[edit] See also

operator=

assigns values to the container adaptor
(public member function) [edit]
std::queue<T,Container>::queue - cppreference.com (2024)

References

Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 5599

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.