Namespaces
Variants

std::stack<T,Container>::stack

ja.cppreference.net より

 
 
 
 
stack() : stack(Container()) {}
(1) (C++11 以上)
explicit stack( const Container& cont = Container() );
(2) (C++11 未満)
explicit stack( const Container& cont );
(C++11 以上)
explicit stack( Container&& cont );
(3) (C++11 以上)
stack( const stack& other );
(4) (暗黙に宣言)
stack( stack&& other );
(5) (C++11 以上)
(暗黙に宣言)
template< class InputIt >
stack( InputIt first, InputIt last );
(6) (C++23 以上)
template< class Alloc >
explicit stack( const Alloc& alloc );
(7) (C++11 以上)
template< class Alloc >
stack( const Container& cont, const Alloc& alloc );
(8) (C++11 以上)
template< class Alloc >
stack( Container&& cont, const Alloc& alloc );
(9) (C++11 以上)
template< class Alloc >
stack( const stack& other, const Alloc& alloc );
(10) (C++11 以上)
template< class Alloc >
stack( stack&& other, const Alloc& alloc );
(11) (C++11 以上)
template< class InputIt, class Alloc >
stack( InputIt first, InputIt last, const Alloc& alloc );
(12) (C++23 以上)
template< container-compatible-range<T> R>
stack( std::from_range_t, R&& rg );
(13) (C++23 以上)
template< container-compatible-range<T> R, class Alloc >
stack( std::from_range_t, R&& rg, const Alloc& alloc );
(14) (C++23 以上)

様々なデータソースから、コンテナアダプタの新しい内部コンテナを構築します。

1) デフォルトコンストラクタ。コンテナを値初期化します。
2) 内部コンテナ c を cont の内容でコピー構築します。 これはデフォルトコンストラクタでもあります。(C++11 未満)
3) 内部コンテナ c を std::move(cont) でムーブ構築します。
4) コピーコンストラクタ。アダプタは other.c の内容でコピー構築されます。
5) ムーブコンストラクタ。アダプタは std::move(other.c) で構築されます。
6) 内部コンテナ c を範囲 [first, last) の内容で構築します。このオーバーロードは、InputIt が LegacyInputIterator を満たす場合にのみオーバーロード解決に参加します。
7-12) これらのコンストラクタは、std::uses_allocator<Container, Alloc>::value が true である場合、つまり内部コンテナがアロケータ対応コンテナである場合(stack で使用可能なすべての標準ライブラリコンテナに当てはまります)にのみオーバーロード解決に参加します。
7) 内部コンテナを alloc をアロケータとして使用して構築します。あたかも c(alloc) によって行われたかのようにします。
8) 内部コンテナを cont の内容と alloc をアロケータとして使用して構築します。あたかも c(cont, alloc) によって行われたかのようにします。
9) 内部コンテナを cont の内容でムーブセマンティクスを使用し、alloc をアロケータとして利用して構築します。あたかも c(std::move(cont), alloc) によって行われたかのようにします。
10) アダプタを other.c の内容と alloc をアロケータとして使用して構築します。あたかも c(other.c, alloc) によって行われたかのようにします。
11) アダプタを other の内容でムーブセマンティクスを使用し、alloc をアロケータとして利用して構築します。あたかも c(std::move(other.c), alloc) によって行われたかのようにします。
12) 内部コンテナを範囲 [first, last) の内容と alloc をアロケータとして使用して構築します。あたかも c(first, last, alloc) によって行われたかのようにします。このオーバーロードは、InputIt が LegacyInputIterator を満たす場合にのみオーバーロード解決に参加します。
13) 内部コンテナを ranges::to<Container>(std::forward<R>(rg)) で構築します。
14) 内部コンテナを ranges::to<Container>(std::forward<R>(rg), alloc) で構築します。

パラメータ

alloc - 基盤となるコンテナの全てのメモリ割り当てに使用するアロケータ
other - 基盤となるコンテナを初期化するためのソースとして使用する別のコンテナアダプタ
cont - 基盤となるコンテナを初期化するためのソースとして使用するコンテナ
first, last - 初期化に使用する要素のソース範囲を定義するイテレータのペア range
rg - container compatible range 、すなわち要素が T に変換可能な input_range
型要件
-
Alloc は Allocator の要件を満たさなければならない
-
Container は Container の要件を満たさなければならない。アロケータパラメータを取るコンストラクタは、 Container が AllocatorAwareContainer の要件を満たす場合にのみオーバーロード解決に参加する
-
InputIt は LegacyInputIterator の要件を満たさなければならない

計算量

ラップされたコンテナに対する対応する操作と同じです。

注記

機能テスト マクロ 値 標準 機能
__cpp_lib_adaptor_iterator_pair_constructor 202106L (C++23) std::queueおよびstd::stackの std::queue と std::stack に対するイテレータペアコンストラクタ; ( 6 ) および ( 12 ) のオーバーロード
__cpp_lib_containers_ranges 202202L (C++23) レンジ対応 の構築と挿入; ( 13 ) および ( 14 ) のオーバーロード

例

#include <cassert>
#include <deque>
#include <iostream>
#include <memory>
#include <ranges>
#include <stack>
int main()
{
    std::stack<int> c1;
    c1.push(5);
    assert(c1.size() == 1);
    std::stack<int> c2(c1);
    assert(c2.size() == 1);
    std::deque<int> deq{3, 1, 4, 1, 5};
    std::stack<int> c3(deq); // オーバーロード (2)
    assert(c3.size() == 5);
# ifdef __cpp_lib_adaptor_iterator_pair_constructor
    const auto il = {2, 7, 1, 8, 2};
    std::stack<int> c4{il.begin(), il.end()}; // C++23, (6)
    assert(c4.size() == 5);
# endif
# if __cpp_lib_containers_ranges >= 202202L
    // C++23, オーバーロード (13)
    auto c5 = std::stack(std::from_range_t, std::ranges::iota(0, 42));
    assert(c5.size() == 42);
    // パイプ構文でも同じ効果、内部的にオーバーロード (13) を使用
    auto c6 = std::ranges::iota(0, 42) | std::ranges::to<std::stack>();
    assert(c6.size() == 42);
    std::allocator<int> alloc;
    // C++23, オーバーロード (14)
    auto c7 = std::stack(std::from_range_t, std::ranges::iota(0, 42), alloc);
    assert(c7.size() == 42);
    // パイプ構文でも同じ効果、内部的にオーバーロード (14) を使用
    auto c8 = std::ranges::iota(0, 42) | std::ranges::to<std::stack>(alloc);
    assert(c8.size() == 42);
# endif
}

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR Applied to Behavior as published Correct behavior
P0935R0 C++11 default constructor was explicit made implicit

関連項目

コンテナアダプタに値を代入する
(公開メンバ関数)