跳到主要内容

std::stack 构造函数

std::stack 类可以通过多种不同方式构造。使用右上角的按钮,方便地通过箭头导航。

注意

std::stack 是一个类模板,具有以下类型参数和成员对象,它们在构造函数中使用。

类型参数

publicT

存储元素的类型。

如果 T 不是 Container::value_type,则行为未定义。 (自 C++17 起)
public容器

用于存储元素的底层容器类型。默认为 std::deque<T>

容器必须满足 SequenceContainer
此外,它必须提供具有通常语义的以下函数

  • back()
  • front()
  • push_back()
  • pop_front()

标准容器 std::dequestd::list 满足这些要求。

成员对象

保护Container c

底层容器。默认情况下为 std::deque<T>

默认构造函数

stack() : stack(Container()) { }

默认构造函数。值初始化底层容器。

复杂度

常数 - O(1)

示例

#include <iostream>
#include <vector>
#include <stack>

int main() {
// Default initialized stack
std::stack<int> s;
std::cout << s.size() << ' ';

// Default initialized stack
std::stack<int, std::vector<int>> s2;
std::cout << s.size() << ' ';
}
结果(控制台)
0 0

复制构造函数

stack( const stack& other );

拷贝构造函数。适配器通过 other.c 的内容进行拷贝构造。

复杂度

线性于 other.c 的大小 - O(other.c.size())

示例

#include <iostream>
#include <deque>
#include <stack>

void present_stack(std::stack<int>& stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
std::deque<int> data = { 1, 2, 3, 4, 5 };
std::stack<int> source(data);
std::stack<int> sink(source);

std::cout << "Original data size: " << data.size() << '\n';

std::cout << "Source size: " << source.size() << '\n';
present_stack(source);
std::cout << "Sink size size: " << sink.size() << '\n';
present_stack(sink);
}
结果(控制台)
Original data size: 5
Source size: 5
5 4 3 2 1
Sink size size: 5
5 4 3 2 1

移动构造函数

stack( stack&& other );

移动构造函数。适配器通过 std::move(other.c) 构造。

复杂度

常数 - O(1)

示例

#include <iostream>
#include <deque>
#include <stack>

void present_stack(std::stack<int>& stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
std::deque<int> data = { 1, 2, 3, 4, 5 };
std::stack<int> source(data);
std::stack<int> sink(std::move(source));

std::cout << "Original data size: " << data.size() << '\n';

std::cout << "Source size: " << source.size() << '\n';
present_stack(source);
std::cout << "Sink size size: " << sink.size() << '\n';
present_stack(sink);
}
结果(控制台)
Original data size: 5
Source size: 0

Sink size size: 5
5 4 3 2 1

拷贝容器构造函数

explicit stack( const Container& cont );

使用 cont 的内容拷贝构造底层容器 c这也是默认构造函数。 (直到 C++11)

复杂度

线性于 cont 的大小 - O(cont.size())

示例

#include <iostream>
#include <deque>
#include <stack>

void present_stack(std::stack<int>& stack) {
std::cout << stack.size() << ' ';

while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}
}

int main() {
std::deque<int> data = { 1, 2, 3, 4, 5 };

std::stack<int> s(data);

std::cout << "Original data size: " << data.size() << '\n';
present_stack(s);
}
结果(控制台)
Original data size: 5
5 5 4 3 2 1

移动容器构造函数

explicit stack( Container&& cont );

使用 std::move(cont) 移动构造底层容器 c

复杂度

常数 - O(1)

示例

#include <iostream>
#include <deque>
#include <stack>

void present_stack(std::stack<int>& stack) {
std::cout << stack.size() << ' ';

while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}
}

int main() {
std::deque<int> data = { 1, 2, 3, 4, 5 };

std::stack<int> s(std::move(data));

std::cout << "Original data size: " << data.size() << '\n';
present_stack(s);
}
结果(控制台)
Original data size: 0
5 5 4 3 2 1

基于范围的构造函数(迭代器)

template< class InputIt >
stack( InputIt first, InputIt last );

使用范围 [ first, last ) 的内容构造底层容器 c

重载决议

此重载仅在以下情况下参与重载解析

复杂度

常数 - O(1)

示例

注意

此代码只能由支持实验性 C++23 模式的编译器编译。

#include <iostream>
#include <vector>
#include <stack>

void present_stack(std::stack<int>& stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
std::vector<int> data = {1, 2, 3, 4, 5};
std::stack<int> s(data.begin(), data.end());

present_stack(s);
}
结果(控制台)
5 4 3 2 1

使用分配器构造函数默认构造底层容器

template< class Alloc >
explicit stack( const Alloc& alloc );

使用 alloc 作为分配器构造底层容器,如同通过 c(alloc)

重载决议

此重载仅在以下情况下参与重载解析

  • std::uses_allocator<Container, Alloc>::valuetrue(即,如果底层容器是支持分配器的容器(所有可与堆栈一起使用的标准库容器都满足此条件))

示例

#include <memory_resource>
#include <iostream>
#include <stack>

using MagicalStack = std::stack<char, std::pmr::deque<char>>;

void present_stack(MagicalStack stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
char message_buffer[128];
std::pmr::monotonic_buffer_resource pool(
std::data(message_buffer),
std::size(message_buffer)
);

MagicalStack s(&pool);
s.push('!');
s.push('i');
s.push('H');

present_stack(s);
}
结果(控制台)
H i ! 

使用分配器拷贝容器构造函数

template< class Alloc >
stack( const Container& cont, const Alloc& alloc );

使用 cont 的内容并使用 alloc 作为分配器构造底层容器,如同通过 c(cont, alloc)

重载决议

此重载仅在以下情况下参与重载解析

  • std::uses_allocator<Container, Alloc>::valuetrue(即,如果底层容器是支持分配器的容器(所有可与堆栈一起使用的标准库容器都满足此条件))

示例

#include <memory_resource>
#include <iostream>
#include <stack>

using MagicalDeque = std::pmr::deque<char>;
using MagicalStack = std::stack<char, MagicalDeque>;

void present_stack(MagicalStack stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
char message_buffer[128];
std::pmr::monotonic_buffer_resource pool(
std::data(message_buffer),
std::size(message_buffer)
);

MagicalDeque d = {'!', 'i', 'H'};
MagicalStack s(d, &pool);

present_stack(s);
}
结果(控制台)
H i ! 

使用分配器移动容器构造函数

template< class Alloc >
stack( Container&& cont, const Alloc& alloc );

使用移动语义并利用 alloc 作为分配器构造底层容器,如同通过 c(std::move(cont), alloc)

重载决议

此重载仅在以下情况下参与重载解析

  • std::uses_allocator<Container, Alloc>::valuetrue(即,如果底层容器是支持分配器的容器(所有可与堆栈一起使用的标准库容器都满足此条件))

示例

#include <memory_resource>
#include <iostream>
#include <stack>

using MagicalDeque = std::pmr::deque<char>;
using MagicalStack = std::stack<char, MagicalDeque>;

void present_stack(MagicalStack stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
char message_buffer[128];
std::pmr::monotonic_buffer_resource pool(
std::data(message_buffer),
std::size(message_buffer)
);

MagicalDeque d = {'!', 'i', 'H'};
MagicalStack s(std::move(d), &pool);

present_stack(s);
}
结果(控制台)
H i ! 

使用分配器拷贝栈构造函数

template< class Alloc >
stack( const stack& other, const Alloc& alloc );

使用 other.c 的内容并使用 alloc 作为分配器构造适配器,如同通过 c(other.c, alloc)

重载决议

此重载仅在以下情况下参与重载解析

  • std::uses_allocator<Container, Alloc>::valuetrue(即,如果底层容器是支持分配器的容器(所有可与堆栈一起使用的标准库容器都满足此条件))

示例

#include <memory_resource>
#include <iostream>
#include <stack>

using MagicalDeque = std::pmr::deque<char>;
using MagicalStack = std::stack<char, MagicalDeque>;

void present_stack(MagicalStack stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
char message_buffer[128];

std::pmr::monotonic_buffer_resource pool(
std::data(message_buffer),
std::size(message_buffer)
);

// uses a new delete source that uses the global new and delete
MagicalStack s(MagicalDeque{ '!', 'i', 'H' }, std::pmr::new_delete_resource());

// now the other stack uses the set monotonic buffer resource
MagicalStack s2(s, &pool);

present_stack(s);
present_stack(s2);
}
结果(控制台)
H i ! 
H i !

使用分配器移动栈构造函数

template< class Alloc >
stack( stack&& other, const Alloc& alloc );

使用移动语义并利用 alloc 作为分配器构造适配器,如同通过 c(std::move(other.c), alloc)

重载决议

此重载仅在以下情况下参与重载解析

  • std::uses_allocator<Container, Alloc>::valuetrue(即,如果底层容器是支持分配器的容器(所有可与堆栈一起使用的标准库容器都满足此条件))

示例

#include <memory_resource>
#include <iostream>
#include <stack>

using MagicalDeque = std::pmr::deque<char>;
using MagicalStack = std::stack<char, MagicalDeque>;

void present_stack(MagicalStack stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
char message_buffer[128];

std::pmr::monotonic_buffer_resource pool(
std::data(message_buffer),
std::size(message_buffer)
);

// uses a new delete source that uses the global new and delete
MagicalStack s(MagicalDeque{ '!', 'i', 'H' }, std::pmr::new_delete_resource());

// now the other stack uses the set monotonic buffer resource
MagicalStack s2(std::move(s), &pool);

present_stack(s);
present_stack(s2);
}
结果(控制台)

H i !

使用分配器基于范围的构造函数(迭代器)

template< class InputIt, class Alloc >
stack( InputIt first, InputIt last, const Alloc& alloc );

使用范围 [ first, last ) 的内容并使用 alloc 作为分配器构造底层容器,如同通过 c(first, last, alloc)

重载决议

此重载仅在以下情况下参与重载解析

示例

注意

此代码只能由支持实验性 C++23 模式的编译器编译。

#include <memory_resource>
#include <iostream>
#include <string>
#include <stack>

using MagicalDeque = std::pmr::deque<char>;
using MagicalStack = std::stack<char, MagicalDeque>;

void present_stack(MagicalStack stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
char message_buffer[128];

std::pmr::monotonic_buffer_resource pool(
std::data(message_buffer),
std::size(message_buffer)
);

std::string s = "!iH";
MagicalStack s2(s.begin(), s.end(), &pool);

std::cout << s << '\n';
present_stack(s2);
}
结果(控制台)
!iH
H i !
本文来源于 此 CppReference 页面。它可能经过修改以进行改进或满足编辑偏好。点击“编辑此页面”以查看本文档所做的所有更改。
悬停查看原始许可证。

std::stack 构造函数

std::stack 类可以通过多种不同方式构造。使用右上角的按钮,方便地通过箭头导航。

注意

std::stack 是一个类模板,具有以下类型参数和成员对象,它们在构造函数中使用。

类型参数

publicT

存储元素的类型。

如果 T 不是 Container::value_type,则行为未定义。 (自 C++17 起)
public容器

用于存储元素的底层容器类型。默认为 std::deque<T>

容器必须满足 SequenceContainer
此外,它必须提供具有通常语义的以下函数

  • back()
  • front()
  • push_back()
  • pop_front()

标准容器 std::dequestd::list 满足这些要求。

成员对象

保护Container c

底层容器。默认情况下为 std::deque<T>

默认构造函数

stack() : stack(Container()) { }

默认构造函数。值初始化底层容器。

复杂度

常数 - O(1)

示例

#include <iostream>
#include <vector>
#include <stack>

int main() {
// Default initialized stack
std::stack<int> s;
std::cout << s.size() << ' ';

// Default initialized stack
std::stack<int, std::vector<int>> s2;
std::cout << s.size() << ' ';
}
结果(控制台)
0 0

复制构造函数

stack( const stack& other );

拷贝构造函数。适配器通过 other.c 的内容进行拷贝构造。

复杂度

线性于 other.c 的大小 - O(other.c.size())

示例

#include <iostream>
#include <deque>
#include <stack>

void present_stack(std::stack<int>& stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
std::deque<int> data = { 1, 2, 3, 4, 5 };
std::stack<int> source(data);
std::stack<int> sink(source);

std::cout << "Original data size: " << data.size() << '\n';

std::cout << "Source size: " << source.size() << '\n';
present_stack(source);
std::cout << "Sink size size: " << sink.size() << '\n';
present_stack(sink);
}
结果(控制台)
Original data size: 5
Source size: 5
5 4 3 2 1
Sink size size: 5
5 4 3 2 1

移动构造函数

stack( stack&& other );

移动构造函数。适配器通过 std::move(other.c) 构造。

复杂度

常数 - O(1)

示例

#include <iostream>
#include <deque>
#include <stack>

void present_stack(std::stack<int>& stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
std::deque<int> data = { 1, 2, 3, 4, 5 };
std::stack<int> source(data);
std::stack<int> sink(std::move(source));

std::cout << "Original data size: " << data.size() << '\n';

std::cout << "Source size: " << source.size() << '\n';
present_stack(source);
std::cout << "Sink size size: " << sink.size() << '\n';
present_stack(sink);
}
结果(控制台)
Original data size: 5
Source size: 0

Sink size size: 5
5 4 3 2 1

拷贝容器构造函数

explicit stack( const Container& cont );

使用 cont 的内容拷贝构造底层容器 c这也是默认构造函数。 (直到 C++11)

复杂度

线性于 cont 的大小 - O(cont.size())

示例

#include <iostream>
#include <deque>
#include <stack>

void present_stack(std::stack<int>& stack) {
std::cout << stack.size() << ' ';

while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}
}

int main() {
std::deque<int> data = { 1, 2, 3, 4, 5 };

std::stack<int> s(data);

std::cout << "Original data size: " << data.size() << '\n';
present_stack(s);
}
结果(控制台)
Original data size: 5
5 5 4 3 2 1

移动容器构造函数

explicit stack( Container&& cont );

使用 std::move(cont) 移动构造底层容器 c

复杂度

常数 - O(1)

示例

#include <iostream>
#include <deque>
#include <stack>

void present_stack(std::stack<int>& stack) {
std::cout << stack.size() << ' ';

while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}
}

int main() {
std::deque<int> data = { 1, 2, 3, 4, 5 };

std::stack<int> s(std::move(data));

std::cout << "Original data size: " << data.size() << '\n';
present_stack(s);
}
结果(控制台)
Original data size: 0
5 5 4 3 2 1

基于范围的构造函数(迭代器)

template< class InputIt >
stack( InputIt first, InputIt last );

使用范围 [ first, last ) 的内容构造底层容器 c

重载决议

此重载仅在以下情况下参与重载解析

复杂度

常数 - O(1)

示例

注意

此代码只能由支持实验性 C++23 模式的编译器编译。

#include <iostream>
#include <vector>
#include <stack>

void present_stack(std::stack<int>& stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
std::vector<int> data = {1, 2, 3, 4, 5};
std::stack<int> s(data.begin(), data.end());

present_stack(s);
}
结果(控制台)
5 4 3 2 1

使用分配器构造函数默认构造底层容器

template< class Alloc >
explicit stack( const Alloc& alloc );

使用 alloc 作为分配器构造底层容器,如同通过 c(alloc)

重载决议

此重载仅在以下情况下参与重载解析

  • std::uses_allocator<Container, Alloc>::valuetrue(即,如果底层容器是支持分配器的容器(所有可与堆栈一起使用的标准库容器都满足此条件))

示例

#include <memory_resource>
#include <iostream>
#include <stack>

using MagicalStack = std::stack<char, std::pmr::deque<char>>;

void present_stack(MagicalStack stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
char message_buffer[128];
std::pmr::monotonic_buffer_resource pool(
std::data(message_buffer),
std::size(message_buffer)
);

MagicalStack s(&pool);
s.push('!');
s.push('i');
s.push('H');

present_stack(s);
}
结果(控制台)
H i ! 

使用分配器拷贝容器构造函数

template< class Alloc >
stack( const Container& cont, const Alloc& alloc );

使用 cont 的内容并使用 alloc 作为分配器构造底层容器,如同通过 c(cont, alloc)

重载决议

此重载仅在以下情况下参与重载解析

  • std::uses_allocator<Container, Alloc>::valuetrue(即,如果底层容器是支持分配器的容器(所有可与堆栈一起使用的标准库容器都满足此条件))

示例

#include <memory_resource>
#include <iostream>
#include <stack>

using MagicalDeque = std::pmr::deque<char>;
using MagicalStack = std::stack<char, MagicalDeque>;

void present_stack(MagicalStack stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
char message_buffer[128];
std::pmr::monotonic_buffer_resource pool(
std::data(message_buffer),
std::size(message_buffer)
);

MagicalDeque d = {'!', 'i', 'H'};
MagicalStack s(d, &pool);

present_stack(s);
}
结果(控制台)
H i ! 

使用分配器移动容器构造函数

template< class Alloc >
stack( Container&& cont, const Alloc& alloc );

使用移动语义并利用 alloc 作为分配器构造底层容器,如同通过 c(std::move(cont), alloc)

重载决议

此重载仅在以下情况下参与重载解析

  • std::uses_allocator<Container, Alloc>::valuetrue(即,如果底层容器是支持分配器的容器(所有可与堆栈一起使用的标准库容器都满足此条件))

示例

#include <memory_resource>
#include <iostream>
#include <stack>

using MagicalDeque = std::pmr::deque<char>;
using MagicalStack = std::stack<char, MagicalDeque>;

void present_stack(MagicalStack stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
char message_buffer[128];
std::pmr::monotonic_buffer_resource pool(
std::data(message_buffer),
std::size(message_buffer)
);

MagicalDeque d = {'!', 'i', 'H'};
MagicalStack s(std::move(d), &pool);

present_stack(s);
}
结果(控制台)
H i ! 

使用分配器拷贝栈构造函数

template< class Alloc >
stack( const stack& other, const Alloc& alloc );

使用 other.c 的内容并使用 alloc 作为分配器构造适配器,如同通过 c(other.c, alloc)

重载决议

此重载仅在以下情况下参与重载解析

  • std::uses_allocator<Container, Alloc>::valuetrue(即,如果底层容器是支持分配器的容器(所有可与堆栈一起使用的标准库容器都满足此条件))

示例

#include <memory_resource>
#include <iostream>
#include <stack>

using MagicalDeque = std::pmr::deque<char>;
using MagicalStack = std::stack<char, MagicalDeque>;

void present_stack(MagicalStack stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
char message_buffer[128];

std::pmr::monotonic_buffer_resource pool(
std::data(message_buffer),
std::size(message_buffer)
);

// uses a new delete source that uses the global new and delete
MagicalStack s(MagicalDeque{ '!', 'i', 'H' }, std::pmr::new_delete_resource());

// now the other stack uses the set monotonic buffer resource
MagicalStack s2(s, &pool);

present_stack(s);
present_stack(s2);
}
结果(控制台)
H i ! 
H i !

使用分配器移动栈构造函数

template< class Alloc >
stack( stack&& other, const Alloc& alloc );

使用移动语义并利用 alloc 作为分配器构造适配器,如同通过 c(std::move(other.c), alloc)

重载决议

此重载仅在以下情况下参与重载解析

  • std::uses_allocator<Container, Alloc>::valuetrue(即,如果底层容器是支持分配器的容器(所有可与堆栈一起使用的标准库容器都满足此条件))

示例

#include <memory_resource>
#include <iostream>
#include <stack>

using MagicalDeque = std::pmr::deque<char>;
using MagicalStack = std::stack<char, MagicalDeque>;

void present_stack(MagicalStack stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
char message_buffer[128];

std::pmr::monotonic_buffer_resource pool(
std::data(message_buffer),
std::size(message_buffer)
);

// uses a new delete source that uses the global new and delete
MagicalStack s(MagicalDeque{ '!', 'i', 'H' }, std::pmr::new_delete_resource());

// now the other stack uses the set monotonic buffer resource
MagicalStack s2(std::move(s), &pool);

present_stack(s);
present_stack(s2);
}
结果(控制台)

H i !

使用分配器基于范围的构造函数(迭代器)

template< class InputIt, class Alloc >
stack( InputIt first, InputIt last, const Alloc& alloc );

使用范围 [ first, last ) 的内容并使用 alloc 作为分配器构造底层容器,如同通过 c(first, last, alloc)

重载决议

此重载仅在以下情况下参与重载解析

示例

注意

此代码只能由支持实验性 C++23 模式的编译器编译。

#include <memory_resource>
#include <iostream>
#include <string>
#include <stack>

using MagicalDeque = std::pmr::deque<char>;
using MagicalStack = std::stack<char, MagicalDeque>;

void present_stack(MagicalStack stack) {
while(stack.size()) {
std::cout << stack.top() << ' ';
stack.pop();
}

std::cout << '\n';
}

int main() {
char message_buffer[128];

std::pmr::monotonic_buffer_resource pool(
std::data(message_buffer),
std::size(message_buffer)
);

std::string s = "!iH";
MagicalStack s2(s.begin(), s.end(), &pool);

std::cout << s << '\n';
present_stack(s2);
}
结果(控制台)
!iH
H i !
本文来源于 此 CppReference 页面。它可能经过修改以进行改进或满足编辑偏好。点击“编辑此页面”以查看本文档所做的所有更改。
悬停查看原始许可证。