std::queue
的推導指南
來自 cppreference.com
定義於標頭檔案 <queue> |
||
template< class Container > queue( Container ) |
(1) | (C++17 起) |
template< class Container, class Alloc > queue( Container, Alloc ) |
(2) | (C++17 起) |
template< class InputIt > queue( InputIt, InputIt ) |
(3) | (C++23 起) |
template< class InputIt, class Alloc > queue( InputIt, InputIt, Alloc ) |
(4) | (C++23 起) |
template< ranges::input_range R > queue( std::from_range_t, R&& ) |
(5) | (C++23 起) |
template< ranges::input_range R, class Allocator > queue( std::from_range_t, R&&, Allocator ) |
(6) | (C++23 起) |
這些推導指南用於 queue
,允許從底層容器型別進行推導。
1) 從引數推導底層容器型別。
2) 與(1)相同,但提供了分配器。
3) 使用 std::deque<typename std::iterator_traits<InputIt>::value_type> 作為底層容器型別,從迭代器推導元素型別。
4) 與(3)相同,但提供了分配器。
6) 與(5)相同,但提供了分配器。
這些過載只有在以下情況下才參與過載決議:
-
InputIt
(如果存在)滿足 LegacyInputIterator, -
Container
(如果存在)不滿足 Allocator, - 對於 (3)(直到 C++23)(4)(自 C++23 起),
Alloc
滿足 Allocator,並且 - 如果
Container
和Alloc
都存在,則 std::uses_allocator_v<Container, Alloc> 為 true。
注意:庫確定型別不滿足 LegacyInputIterator 的程度未指定,但至少整數型別不符合輸入迭代器的條件。同樣,庫確定型別不滿足 Allocator 的程度未指定,但至少成員型別 Alloc::value_type
必須存在,並且表示式 std::declval<Alloc&>().allocate(std::size_t{}) 在作為未求值運算元處理時必須是格式良好的。
[編輯] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_adaptor_iterator_pair_constructor |
202106L |
(C++23) | std::queue 和 std::stack 的迭代器對建構函式;過載 (2) 和 (4) |
__cpp_lib_containers_ranges |
202202L |
(C++23) | 範圍感知構造和插入;過載 (5) 和 (6) |
[編輯] 示例
執行此程式碼
#include <queue> #include <vector> int main() { std::vector<int> v = {1, 2, 3, 4}; std::queue s{v}; // guide #1 deduces std::queue<int, vector<int>> }