Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::priority_queue<T,Container,Compare>::priority_queue

      From cppreference.com
      <cpp‎ |container‎ |priority queue
       
       
       
       
      priority_queue(): priority_queue(Compare(), Container()){}
      (1)(since C++11)
      explicit priority_queue(const Compare& compare)
         : priority_queue(compare, Container()){}
      (2)(since C++11)
      (3)
      explicit priority_queue(const Compare& compare= Compare(),
                               const Container& cont= Container());
      (until C++11)
      priority_queue(const Compare& compare,const Container& cont);
      (since C++11)
      priority_queue(const Compare& compare, Container&& cont);
      (4)(since C++11)
      priority_queue(const priority_queue& other);
      (5)
      priority_queue( priority_queue&& other);
      (6)(since C++11)
      template<class InputIt>

      priority_queue( InputIt first, InputIt last,

                     const Compare& compare= Compare());
      (7)(since C++11)
      (8)
      template<class InputIt>

      priority_queue( InputIt first, InputIt last,
                     const Compare& compare= Compare(),

                     const Container& cont= Container());
      (until C++11)
      template<class InputIt>

      priority_queue( InputIt first, InputIt last,

                     const Compare& compare,const Container& cont);
      (since C++11)
      template<class InputIt>

      priority_queue( InputIt first, InputIt last,

                     const Compare& compare, Container&& cont);
      (9)(since C++11)
      template<class Alloc>
      explicit priority_queue(const Alloc& alloc);
      (10)(since C++11)
      template<class Alloc>
      priority_queue(const Compare& compare,const Alloc& alloc);
      (11)(since C++11)
      template<class Alloc>

      priority_queue(const Compare& compare,const Container& cont,

                     const Alloc& alloc);
      (12)(since C++11)
      template<class Alloc>

      priority_queue(const Compare& compare, Container&& cont,

                     const Alloc& alloc);
      (13)(since C++11)
      template<class Alloc>
      priority_queue(const priority_queue& other,const Alloc& alloc);
      (14)(since C++11)
      template<class Alloc>
      priority_queue( priority_queue&& other,const Alloc& alloc);
      (15)(since C++11)
      template<class InputIt,class Alloc>
      priority_queue( InputIt first, InputIt last,const Alloc& alloc);
      (16)(since C++11)
      template<class InputIt,class Alloc>

      priority_queue( InputIt first, InputIt last,const Compare& compare,

                     const Alloc& alloc);
      (17)(since C++11)
      template<class InputIt,class Alloc>

      priority_queue( InputIt first, InputIt last,const Compare& compare,

                     const Container& cont,const Alloc& alloc);
      (18)(since C++11)
      template<class InputIt,class Alloc>

      priority_queue( InputIt first, InputIt last,const Compare& compare,

                      Container&& cont,const Alloc& alloc);
      (19)(since C++11)
      template<container-compatible-range<T> R>

      priority_queue(std::from_range_t, R&& rg,

                     const Compare& compare= Compare());
      (20)(since C++23)
      template<container-compatible-range<T> R,class Alloc>

      priority_queue(std::from_range_t, R&& rg,

                     const Compare& compare,const Alloc& alloc);
      (21)(since C++23)
      template<container-compatible-range<T> R,class Alloc>
      priority_queue(std::from_range_t, R&& rg,const Alloc& alloc);
      (22)(since C++23)

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

      1) Default constructor. Value-initializes the comparator and the underlying container.
      2) Copy-constructs the comparison functorcomp with the contents ofcompare. Value-initializes the underlying containerc.
      3) Copy-constructs the underlying containerc with the contents ofcont. Copy-constructs the comparison functorcomp with the contents ofcompare. Callsstd::make_heap(c.begin(), c.end(), comp).This is also the default constructor.(until C++11)
      4) Move-constructs the underlying containerc withstd::move(cont). Copy-constructs the comparison functorcomp withcompare. Callsstd::make_heap(c.begin(), c.end(), comp).
      5)Copy constructor. The underlying container is copy-constructed withother.c. The comparison functor is copy-constructed withother.comp.(implicitly declared)
      6)Move constructor. The underlying container is constructed withstd::move(other.c). The comparison functor is constructed withstd::move(other.comp).(implicitly declared)
      7-9) Iterator-pair constructors. These overloads participate in overload resolution only ifInputIt satisfiesLegacyInputIterator.
      7) Constructsc as if byc(first, last) andcomp fromcompare. Then callsstd::make_heap(c.begin(), c.end(), comp);.
      8) Copy-constructsc fromcont andcomp fromcompare. Then callsc.insert(c.end(), first, last);, and then callsstd::make_heap(c.begin(), c.end(), comp);.
      9) Move-constructsc fromstd::move(cont) and copy-constructscomp fromcompare. Then callsc.insert(c.end(), first, last);, and then callsstd::make_heap(c.begin(), c.end(), comp);.
      10-15) Allocator-extended constructors. These overloads participate in overload resolution only ifstd::uses_allocator<container_type, Alloc>::value istrue, that is, if the underlying container is an allocator-aware container (true for all standard library containers).
      10) Constructs the underlying container usingalloc as allocator. Effectively callsc(alloc).comp is value-initialized.
      11) Constructs the underlying container usingalloc as allocator. Effectively callsc(alloc). Copy-constructscomp fromcompare.
      12) Constructs the underlying container with the contents ofcont and usingalloc as allocator, as if byc(cont, alloc). Copy-constructscomp fromcompare. Then callsstd::make_heap(c.begin(), c.end(), comp).
      13) Constructs the underlying container with the contents ofcont using move semantics while usingalloc as allocator, as if byc(std::move(cont), alloc). Copy-constructscomp fromcompare. Then callsstd::make_heap(c.begin(), c.end(), comp).
      14) Constructs the underlying container with the contents ofother.c and usingalloc as allocator. Effectively callsc(other.c, alloc). Copy-constructscomp fromother.comp.
      15) Constructs the underlying container with the contents ofother using move semantics while utilizingalloc as allocator. Effectively callsc(std::move(other.c), alloc). Move-constructscomp fromother.comp.
      16-19) Allocator-extended iterator-pair constructors. Same as(7-9), except thatalloc is used for constructing the underlying container. These overloads participate in overload resolution only ifstd::uses_allocator<container_type, Alloc>::value istrue andInputIt satisfiesLegacyInputIterator.
      20) Initializescomp withcompare andc withranges::to<Container>(std::forward<R>(rg)). Then callsstd::make_heap(c.begin(), c.end(), comp).
      21) Initializescomp withcompare andc withranges::to<Container>(std::forward<R>(rg), alloc). Then callsstd::make_heap(c.begin(), c.end(), comp).
      22) Initializesc withranges::to<Container>(std::forward<R>(rg), alloc). Then callsstd::make_heap(c.begin(), c.end(), comp).

      Note that how an implementation checks whether a type satisfiesLegacyInputIterator is unspecified, except that integral types are required to be rejected.

      Contents

      [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
      compare - the comparison function object to initialize the underlying comparison functor
      first, last - the pair of iterators defining therange of elements to initialize with
      rg - acontainer compatible range, that is, aninput_range whose elements are convertible toT
      Type requirements
      -
      Alloc must meet the requirements ofAllocator.
      -
      Compare must meet the requirements ofCompare.
      -
      Container must meet the requirements ofContainer. The allocator-extended constructors are only defined ifContainer meets the requirements ofAllocatorAwareContainer.
      -
      InputIt must meet the requirements ofLegacyInputIterator.

      [edit]Complexity

      1,2) Constant.
      3,5,12)\(\scriptsize \mathcal{O}{(N)}\)O(N) comparisons and\(\scriptsize \mathcal{O}{(N)}\)O(N) calls to the constructor ofvalue_type, where\(\scriptsize N\)N iscont.size().
      4)\(\scriptsize \mathcal{O}{(N)}\)O(N) comparisons, where\(\scriptsize N\)N iscont.size().
      6) Constant.
      7,16,17)\(\scriptsize \mathcal{O}{(M)}\)O(M) comparisons, where\(\scriptsize M\)M isstd::distance(first, last).
      8,18)\(\scriptsize \mathcal{O}{(N + M)}\)O(N + M) comparisons and\(\scriptsize \mathcal{O}{(N)}\)O(N) calls to the constructor ofvalue_type, where\(\scriptsize N\)N iscont.size() and\(\scriptsize M\)M isstd::distance(first, last).
      9)\(\scriptsize \mathcal{O}{(N + M)}\)O(N + M) comparisons, where\(\scriptsize N\)N iscont.size() and\(\scriptsize M\)M isstd::distance(first, last).
      10,11) Constant.
      13)\(\scriptsize \mathcal{O}{(N)}\)O(N) comparisons, where\(\scriptsize N\)N iscont.size().
      14) Linear in size ofother.
      15) Constant ifAlloc compares equal to the allocator ofother. Linear in size ofother otherwise.
      19)\(\scriptsize \mathcal{O}{(N + M)}\)O(N + M) comparisons and possibly\(\scriptsize \mathcal{O}{(N)}\)O(N) calls to the constructor ofvalue_type (present ifAlloc does not compare equal to the allocator ofother), where\(\scriptsize N\)N iscont.size() and\(\scriptsize M\)M isstd::distance(first, last).
      20)\(\scriptsize \mathcal{O}{(N)}\)O(N) comparisons and\(\scriptsize \mathcal{O}{(N)}\)O(N) calls to the constructor ofvalue_type, where\(\scriptsize N\)N isranges::distance(rg).
      21,22)
      This section is incomplete

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_containers_ranges202202L(C++23)Ranges-aware construction and insertion; overloads(20-22)

      [edit]Example

      Run this code
      #include <complex>#include <functional>#include <iostream>#include <queue>#include <vector> int main(){std::priority_queue<int> pq1;    pq1.push(5);std::cout<<"pq1.size() = "<< pq1.size()<<'\n'; std::priority_queue<int> pq2{pq1};std::cout<<"pq2.size() = "<< pq2.size()<<'\n'; std::vector<int> vec{3,1,4,1,5};std::priority_queue<int> pq3{std::less<int>(), vec};std::cout<<"pq3.size() = "<< pq3.size()<<'\n'; for(std::cout<<"pq3 : ";!pq3.empty(); pq3.pop())std::cout<< pq3.top()<<' ';std::cout<<'\n'; // Demo With Custom Comparator: using my_value_t=std::complex<double>;using my_container_t=std::vector<my_value_t>; auto my_comp=[](const my_value_t& z1,const my_value_t& z2){return z2.real()< z1.real();}; std::priority_queue<my_value_t,                        my_container_t,                        decltype(my_comp)> pq4{my_comp}; usingnamespace std::complex_literals;    pq4.push(5.0+ 1i);    pq4.push(3.0+ 2i);    pq4.push(7.0+ 3i); for(;!pq4.empty(); pq4.pop()){constauto& z= pq4.top();std::cout<<"pq4.top() = "<< z<<'\n';} // TODO: C++23 range-aware ctors}

      Output:

      pq1.size() = 1pq2.size() = 1pq3.size() = 5pq3 : 5 4 3 1 1pq4.top() = (3,2)pq4.top() = (5,1)pq4.top() = (7,3)

      [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 and constructor(4) were explicitmade implicit
      LWG 3506C++11allocator-extended iterator-pair constructors were missingadded
      LWG 3522C++11constraints on iterator-pair constructors were missingadded
      LWG 3529C++11construction from a pair of iterators calledinsertconstructs the container from them

      [edit]See also

      assigns values to the container adaptor
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/priority_queue/priority_queue&oldid=180831"

      [8]ページ先頭

      ©2009-2025 Movatter.jp