CppDS.com

C++ 98 11 14 17 20 手册

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

来自cppreference.com
< cpp‎ | container‎ | stack

stack() : stack(Container()) { }
(1) (C++11 起)
(2)
explicit stack( const Container& cont = Container() );
(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 Alloc >
explicit stack( const Alloc& alloc );
(6) (C++11 起)
template< class Alloc >
stack( const Container& cont, const Alloc& alloc );
(7) (C++11 起)
template< class Alloc >
stack( Container&& cont, const Alloc& alloc );
(8) (C++11 起)
template< class Alloc >
stack( const stack& other, const Alloc& alloc );
(9) (C++11 起)
template< class Alloc >
stack( stack&& other, const Alloc& alloc );
(10) (C++11 起)

从各种数据源构造容器适配器的新底层容器。

1) 默认构造函数。值初始化容器。
2)cont 的内容复制构造底层容器 c此亦为默认构造函数。 (C++11 前)
3)std::move(cont) 移动构造底层容器 c
4) 复制构造函数。适配器以 other.c 的内容复制构造。(隐式声明)
5) 移动构造函数。适配器以 std::move(other.c) 构造。(隐式声明)
6-10) 仅若 std::uses_allocator<container_type, Alloc>::value == true ,即底层容器是具分配器容器(对所有标准库容器为 true )才定义下列构造函数。
6)alloc 为分配器构造底层容器,如同以 c(alloc)
7)cont 的内容,并以 alloc 为分配器构造底层容器,如同以 c(cont, alloc)
8)cont 的内容用移动语义,同时以 alloc 为分配器构造底层容器,如同以 c(std::move(cont), alloc)
9)other.c 的内容,并以 alloc 为分配器构造适配器,如同以 c(other.c, alloc)
10)other 的内容使用移动语义,并以 alloc 为分配器构造适配器,如同以 c(std::move(other.c), alloc)

参数

alloc - 用于底层容器所有内存分配的分配器
other - 用作源初始化底层容器的另一容器适配器
cont - 用作源初始化底层容器的容器
first, last - 用以初始化的元素
类型要求
-
Alloc 必须满足分配器 (Allocator) 的要求。
-
Container 必须满足容器 (Container) 的要求。仅若 Container 满足知分配器容器 (AllocatorAwareContainer) 的要求才定义构造函数 (5-10)
-
InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。

复杂度

同被包装容器上的对应操作。

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

DR 应用于 出版时的行为 正确行为
P0935R0 C++11 默认构造函数曾为 explicit 使之为隐式

示例

#include <stack>
#include <deque>
#include <iostream>
 
int main()
{
    std::stack<int> c1;
    c1.push(5);
    std::cout << c1.size() << '\n';
 
    std::stack<int> c2(c1);
    std::cout << c2.size() << '\n';
 
    std::deque<int> deq {3, 1, 4, 1, 5};
    std::stack<int> c3(deq);
    std::cout << c3.size() << '\n';
}

输出:

1
1
5

参阅

赋值给容器适配器
(公开成员函数)
关闭