std::stack::stack
来自cppreference.com
(1) | ||
explicit stack( const Container& cont = Container() ); |
(C++11 前) | |
explicit stack( const Container& cont ); |
(C++11 起) | |
explicit stack( Container&& cont = Container() ); |
(2) | (C++11 起) |
stack( const stack& other ); |
(3) | |
stack( stack&& other ); |
(4) | (C++11 起) |
template< class Alloc > explicit stack( const Alloc& alloc ); |
(5) | (C++11 起) |
template< class Alloc > stack( const Container& cont, const Alloc& alloc ); |
(6) | (C++11 起) |
template< class Alloc > stack( Container&& cont, const Alloc& alloc ); |
(7) | (C++11 起) |
template< class Alloc > stack( const stack& other, const Alloc& alloc ); |
(8) | (C++11 起) |
template< class Alloc > stack( stack&& other, const Alloc& alloc ); |
(9) | (C++11 起) |
从各种数据源构造容器适配器的新底层容器。
1) 以
cont
的内容复制构造底层容器 c
。此亦为默认构造函数。 (C++11 前)2) 以 std::move(cont) 移动构造底层容器
c
。此亦为默认构造函数。 (C++11 起)3) 复制构造函数。适配器以 other.c 的内容复制构造。(隐式声明)
4) 移动构造函数。适配器以 std::move(other.c) 构造。(隐式声明)
5-9) 仅若 std::uses_allocator<container_type, Alloc>::value == true ,即底层容器是具分配器容器(对所有标准库容器为 true )才定义下列构造函数。
5) 以
alloc
为分配器构造底层容器,如同以 c(alloc) 。6) 用
cont
的内容,并以 alloc
为分配器构造底层容器,如同以 c(cont, alloc) 。7) 以
cont
的内容用移动语义,同时以 alloc
为分配器构造底层容器,如同以 c(std::move(cont), alloc) 。8) 以
other.c
的内容,并以 alloc
为分配器构造适配器,如同以 c(other.c, alloc) 。9) 以
other
的内容使用移动语义,并以 alloc
为分配器构造适配器,如同以 c(std::move(other.c), alloc) 。目录 |
[编辑] 参数
alloc | - | 用于底层容器所有内存分配的分配器 |
other | - | 用作源初始化底层容器的另一容器适配器 |
cont | - | 用作源初始化底层容器的容器 |
first, last | - | 用以初始化的元素 |
类型要求 | ||
-Alloc 必须满足 Allocator 的要求。
| ||
-Container 必须满足 Container 的要求。仅若 Container 满足具分配器容器 (AllocatorAwareContainer ) 的要求才定义构造函数 (5-10)
| ||
-InputIt 必须满足 InputIterator 的要求。
|
[编辑] 复杂度
1, 3, 5, 6, 8 :与 cont
或 other
成线性
2, 4, 7, 9 :常数
本节未完成 |
[编辑] 示例
运行此代码
#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
[编辑] 参阅
赋值给容器适配器 (公开成员函数) |