std::priority_queue<T,Container,Compare>::push_range
template< container-compatible-range<value_type> R > void push_range( R&& rg ); | (since C++23) |
Inserts a copy of each element of rg in priority_queue, as if by:
-
c.append_range(std::forward<R>(rg))if that is a valid expression (i.e. the underlying containerchas an appropriateappend_rangemember function), or -
ranges::copy(rg, std::back_inserter(c))otherwise.
Then restores the heap property as if by ranges::make_heap(c, comp). After the insertion ranges::is_heap(c, comp) is true.
Each iterator in the range rg is dereferenced exactly once.
Parameters
| rg | - | a container compatible range, that is, an input_range whose elements are convertible to T |
Complexity
The complexity of c.append_range plus the complexity of ranges::make_heap(c, comp).
Notes
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_containers_ranges | 202202L | (C++23) | Ranges-aware construction and insertion |
Example
#include <initializer_list>
#include <queue>
#include <version>
#ifdef __cpp_lib_format_ranges
#include <print>
using std::println;
#else
#define FMT_HEADER_ONLY
#include <fmt/ranges.h>
using fmt::println;
#endif
int main()
{
std::priority_queue<int> adaptor;
const auto rg = {1, 3, 2, 4};
#ifdef __cpp_lib_containers_ranges
adaptor.push_range(rg);
#else
for (int e : rg)
adaptor.push(e);
#endif
println("{}", adaptor);
}
Output:
[4, 3, 2, 1]
See also
| inserts element and sorts the underlying container (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/container/priority_queue/push_range