Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::queue

      From cppreference.com
      <cpp‎ |container
       
       
       
       
      Defined in header<queue>
      template<

         class T,
         class Container=std::deque<T>

      >class queue;

      Thestd::queue class template is acontainer adaptor that gives the functionality of aqueue - specifically, a FIFO (first-in, first-out) data structure.

      The class template acts as a wrapper to the underlying container - only a specific set of functions is provided. The queue pushes the elements on the back of the underlying container and pops them from the front.

      All member functions ofstd::queue areconstexpr: it is possible to create and usestd::queue objects in the evaluation of a constant expression.

      However,std::queue objects generally cannot beconstexpr, because any dynamically allocated storage must be released in the same evaluation of constant expression.

      (since C++26)

      Contents

      [edit]Template parameters

      T - The type of the stored elements. The program is ill-formed ifT is not the same type asContainer::value_type.
      Container - The type of the underlying container to use to store the elements. The container must satisfy the requirements ofSequenceContainer. Additionally, it must provide the following functions with theusual semantics:

      The standard containersstd::deque andstd::list satisfy these requirements.

      [edit]Member types

      Member type Definition
      container_typeContainer[edit]
      value_typeContainer::value_type[edit]
      size_typeContainer::size_type[edit]
      referenceContainer::reference[edit]
      const_referenceContainer::const_reference[edit]

      [edit]Member objects

      Member name Definition
      Container c
      the underlying container
      (protected member object)[edit]

      [edit]Member functions

      constructs thequeue
      (public member function)[edit]
      destructs thequeue
      (public member function)[edit]
      assigns values to the container adaptor
      (public member function)[edit]
      Element access
      access the first element
      (public member function)[edit]
      access the last element
      (public member function)[edit]
      Capacity
      checks whether the container adaptor is empty
      (public member function)[edit]
      returns the number of elements
      (public member function)[edit]
      Modifiers
      inserts element at the end
      (public member function)[edit]
      (C++23)
      inserts a range of elements at the end
      (public member function)[edit]
      (C++11)
      constructs element in-place at the end
      (public member function)[edit]
      removes the first element
      (public member function)[edit]
      (C++11)
      swaps the contents
      (public member function)[edit]

      [edit]Non-member functions

      lexicographically compares the values of twoqueues
      (function template)[edit]
      specializes thestd::swap algorithm
      (function template)[edit]

      [edit]Helper classes

      specializes thestd::uses_allocator type trait
      (class template specialization)[edit]
      formatting support forstd::queue
      (class template specialization)[edit]

      Deduction guides

      (since C++17)

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_containers_ranges202202L(C++23)Ranges construction and insertion for containers
      __cpp_lib_constexpr_queue202502L(C++26)constexprstd::queue

      [edit]Example

      Run this code
      #include <cassert>#include <iostream>#include <queue> int main(){    std::queue<int> q;     q.push(0);// back pushes 0    q.push(1);// q = 0 1    q.push(2);// q = 0 1 2    q.push(3);// q = 0 1 2 3 assert(q.front()==0);assert(q.back()==3);assert(q.size()==4);     q.pop();// removes the front element, 0assert(q.size()==3); // Print and remove all elements. Note that std::queue does not// support begin()/end(), so a range-for-loop cannot be used.std::cout<<"q: ";for(;!q.empty(); q.pop())std::cout<< q.front()<<' ';std::cout<<'\n';assert(q.size()==0);}

      Output:

      q: 1 2 3

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 307C++98std::queue did not support containers using proxy
      reference types[1] in place of (const)value_type&
      supported
      LWG 2566C++98Missing the requirement forContainer::value_typeill-formed ifT is not the same type asContainer::value_type
      1. Such as containers similar tostd::vector<bool> with additional support ofpop_front(). The resolution of this DR
        added support ofstd::vector<bool> forstd::stack andstd::priority_queue. The changes involvingstd::queue
        are for maintaining consistency.

      [edit]See also

      adapts a container to provide priority queue
      (class template)[edit]
      double-ended queue
      (class template)[edit]
      doubly-linked list
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/queue&oldid=182867"

      [8]ページ先頭

      ©2009-2025 Movatter.jp