Difference between revisions of "cpp/container/priority queue/priority queue"
m (r2.7.3) (Robot: Adding de, fr, it, pt, zh) |
|||
Line 106: | Line 106: | ||
| code= | | code= | ||
#include <queue> | #include <queue> | ||
− | #include < | + | #include <vector> |
#include <iostream> | #include <iostream> | ||
+ | #include <functional> | ||
int main() | int main() | ||
Line 118: | Line 119: | ||
std::cout << c2.size() << '\n'; | std::cout << c2.size() << '\n'; | ||
− | std:: | + | std::vector<int> vec={3, 1, 4, 1, 5}; |
− | std::priority_queue<int> c3(std::less<int>(), | + | std::priority_queue<int> c3(std::less<int>(), vec); |
std::cout << c3.size() << '\n'; | std::cout << c3.size() << '\n'; | ||
} | } |
Revision as of 04:41, 15 January 2013
Template:ddcl list begin <tr class="t-dcl ">
<td > const Container& cont = Container());
<td > (1) </td>
<td > (until C++11)
(since C++11) </td>
</tr>
<tr class="t-dcl ">
Container&& cont = Container());
<td > (2) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">
<td ><td > (3) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">
<td ><td > (4) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">
<td >explicit priority_queue( const Alloc& alloc );
<td > (5) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">
<td >priority_queue( const Compare& compare, const Alloc& alloc );
<td > (6) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">
<td >priority_queue( const Compare& compare, const Container& cont,
<td > (7) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">
<td >priority_queue( const Compare& compare, Container&& cont,
<td > (8) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">
<td >priority_queue( const priority_queue& other, const Alloc& alloc );
<td > (9) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">
<td >priority_queue( priority_queue&& other, const Alloc& alloc );
<td > (10) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">
<td >priority_queue( InputIt first, InputIt last,
<td > (11) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">
<td >priority_queue(InputIt first, InputIt last,
<td > (12) </td> <td > (since C++11) </td> </tr> Template:ddcl list end
Constructs new underlying container of the container adaptor from a variety of data sources.
c
with the contents of cont
. Copy-constructs the comparison functor comp
with the contents of compare
. Calls std::make_heap(c.begin(), c.end(), comp).}} This is also the default constructor (until C++11)c
with std::move(cont). Move-constructs the comparison functor comp
with std::move(compare). Calls std::make_heap(c.begin(), c.end(), comp). This is also the default constructor (since C++11)alloc
as allocator. Effectively calls c(alloc). comp
is value-initialized.alloc
as allocator. Effectively calls c(alloc). Copy-constructs comp
from compare
.cont
and using alloc
as allocator. Effectively calls c(cont, alloc). Copy-constructs comp
from compare
.cont
using move semantics while utilising alloc
as allocator. Effectively calls c(std::move(cont), alloc). Copy-constructs comp
from compare
.other.c
and using alloc
as allocator. Effectively calls c(athor.c, alloc). Copy-constructs comp
from other.comp
.other
using move semantics while utilising alloc
as allocator. Effectively calls c(std::move(other.c), alloc). Move-constructs comp
from other.comp
.c
from cont
and comp
from compare
. Then calls c.insert(c.end(), first, last);, and then calls std::make_heap(c.begin(), c.end(), comp);.c
from std::move(cont)
and comp
from std::move(compare)
. Then calls c.insert(c.end(), first, last);, and then calls std::make_heap(c.begin(), c.end(), comp);.Contents |
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 | - | range of elements to initialize with |
Type requirements | ||
-Alloc must meet the requirements of Allocator.
| ||
-Container must meet the requirements of Container. The constructors (5-10) are only defined if Container meets the requirements of Template:concept
| ||
-InputIt must meet the requirements of LegacyInputIterator.
|
Complexity
1, 3, 5, 6, 8: linear in cont
or other
2, 4, 7, 9: constant
This section is incomplete |
Example
#include <queue> #include <vector> #include <iostream> #include <functional> int main() { std::priority_queue<int> c1; c1.push(5); std::cout << c1.size() << '\n'; std::priority_queue<int> c2(c1); std::cout << c2.size() << '\n'; std::vector<int> vec={3, 1, 4, 1, 5}; std::priority_queue<int> c3(std::less<int>(), vec); std::cout << c3.size() << '\n'; }
Output:
1 1 5