Skip to main content

std::stack constructors

std::stack class can be constructed in many different ways. Use the button in the top-right corner to navigate with arrows for convenience.

note

std::stack is a class template, with following type parameters and member objects that are used within constructors:

Type parameters

pubT

The type of the stored elements.

The behaviour is undefined if T is not Container::value_type. (since C++17)
pubContainer

The type of the underlying container used to store the elements. By default std::deque<T>.

The container must satisfy SequenceContainer
Additionally, it must provide the following functions with the usual semantics:

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

The standard containers std::deque and std::list satisfy these requirements.

Member objects

protContainer c

The underlying container. By default std::deque<T>.

Default constructor

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

Default constructor. Value initializes the underlying container.

Complexity

Constant - O(1).

Example

#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() << ' ';
}
Result (console)
0 0

Copy constructor

stack( const stack& other );

Copy constructor. The adaptor is copy-constructed with the contents of other.c.

Complexity

Linear in the size of other.c - O(other.c.size()).

Example

#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);
}
Result (console)
Original data size: 5
Source size: 5
5 4 3 2 1
Sink size size: 5
5 4 3 2 1

Move constructor

stack( stack&& other );

Move constructor. The adaptor is constructed with std::move(other.c).

Complexity

Constant - O(1).

Example

#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);
}
Result (console)
Original data size: 5
Source size: 0

Sink size size: 5
5 4 3 2 1

Copy container constructor

explicit stack( const Container& cont );

Copy-constructs the underlying container c with the contents of cont. This is also the default constructor. (until C++11)

Complexity

Linear in the size of cont - O(cont.size()).

Example

#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);
}
Result (console)
Original data size: 5
5 5 4 3 2 1

Move container constructor

explicit stack( Container&& cont );

Move-constructs the underlying container c with std::move(cont).

Complexity

Constant - O(1).

Example

#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);
}
Result (console)
Original data size: 0
5 5 4 3 2 1

Range based constructor (iterators)

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

Constructs the underlying container c with the contents of the range [ first, last ).

Overload resolution

This overload participates in overload resolution only if:

Complexity

Constant - O(1).

Example

caution

This code can only be possibly compiled by a compiler that can support experimental C++23 mode.

#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);
}
Result (console)
5 4 3 2 1

Default construct underlying container with allocator constructor

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

Constructs the underlying container using alloc as allocator, as if by c(alloc).

Overload resolution

This overload participates in overload resolution only if:

  • std::uses_allocator<Container, Alloc>::value is true (that is, if the underlying container is an allocator-aware container (true for all standard library containers that can be used with stack))

Example

#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);
}
Result (console)
H i ! 

Copy container with allocator constructor

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

Constructs the underlying container with the contents of cont and using alloc as allocator, as if by c(cont, alloc).

Overload resolution

This overload participates in overload resolution only if:

  • std::uses_allocator<Container, Alloc>::value is true (that is, if the underlying container is an allocator-aware container (true for all standard library containers that can be used with stack))

Example

#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);
}
Result (console)
H i ! 

Move container with allocator constructor

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

Constructs the underlying container with the contents of cont using move semantics while utilizing alloc as allocator, as if by c(std::move(cont), alloc).

Overload resolution

This overload participates in overload resolution only if:

  • std::uses_allocator<Container, Alloc>::value is true (that is, if the underlying container is an allocator-aware container (true for all standard library containers that can be used with stack))

Example

#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);
}
Result (console)
H i ! 

Copy stack with allocator constructor

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

Constructs the adaptor with the contents of other.c and using alloc as allocator, as if by c(other.c, alloc).

Overload resolution

This overload participates in overload resolution only if:

  • std::uses_allocator<Container, Alloc>::value is true (that is, if the underlying container is an allocator-aware container (true for all standard library containers that can be used with stack))

Example

#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);
}
Result (console)
H i ! 
H i !

Move stack with allocator constructor

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

Constructs the adaptor with the contents of other using move semantics while utilizing alloc as allocator, as if by c(std::move(other.c), alloc).

Overload resolution

This overload participates in overload resolution only if:

  • std::uses_allocator<Container, Alloc>::value is true (that is, if the underlying container is an allocator-aware container (true for all standard library containers that can be used with stack))

Example

#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);
}
Result (console)

H i !

Range based constructor with allocator (iterators)

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

Constructs the underlying container with the contents of the range [ first, last ) using alloc as allocator, as if by c(first, last, alloc).

Overload resolution

This overload participates in overload resolution only if:

Example

caution

This code can only be possibly compiled by a compiler that can support experimental C++23 mode.

#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);
}
Result (console)
!iH
H i !
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::stack constructors

std::stack class can be constructed in many different ways. Use the button in the top-right corner to navigate with arrows for convenience.

note

std::stack is a class template, with following type parameters and member objects that are used within constructors:

Type parameters

pubT

The type of the stored elements.

The behaviour is undefined if T is not Container::value_type. (since C++17)
pubContainer

The type of the underlying container used to store the elements. By default std::deque<T>.

The container must satisfy SequenceContainer
Additionally, it must provide the following functions with the usual semantics:

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

The standard containers std::deque and std::list satisfy these requirements.

Member objects

protContainer c

The underlying container. By default std::deque<T>.

Default constructor

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

Default constructor. Value initializes the underlying container.

Complexity

Constant - O(1).

Example

#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() << ' ';
}
Result (console)
0 0

Copy constructor

stack( const stack& other );

Copy constructor. The adaptor is copy-constructed with the contents of other.c.

Complexity

Linear in the size of other.c - O(other.c.size()).

Example

#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);
}
Result (console)
Original data size: 5
Source size: 5
5 4 3 2 1
Sink size size: 5
5 4 3 2 1

Move constructor

stack( stack&& other );

Move constructor. The adaptor is constructed with std::move(other.c).

Complexity

Constant - O(1).

Example

#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);
}
Result (console)
Original data size: 5
Source size: 0

Sink size size: 5
5 4 3 2 1

Copy container constructor

explicit stack( const Container& cont );

Copy-constructs the underlying container c with the contents of cont. This is also the default constructor. (until C++11)

Complexity

Linear in the size of cont - O(cont.size()).

Example

#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);
}
Result (console)
Original data size: 5
5 5 4 3 2 1

Move container constructor

explicit stack( Container&& cont );

Move-constructs the underlying container c with std::move(cont).

Complexity

Constant - O(1).

Example

#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);
}
Result (console)
Original data size: 0
5 5 4 3 2 1

Range based constructor (iterators)

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

Constructs the underlying container c with the contents of the range [ first, last ).

Overload resolution

This overload participates in overload resolution only if:

Complexity

Constant - O(1).

Example

caution

This code can only be possibly compiled by a compiler that can support experimental C++23 mode.

#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);
}
Result (console)
5 4 3 2 1

Default construct underlying container with allocator constructor

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

Constructs the underlying container using alloc as allocator, as if by c(alloc).

Overload resolution

This overload participates in overload resolution only if:

  • std::uses_allocator<Container, Alloc>::value is true (that is, if the underlying container is an allocator-aware container (true for all standard library containers that can be used with stack))

Example

#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);
}
Result (console)
H i ! 

Copy container with allocator constructor

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

Constructs the underlying container with the contents of cont and using alloc as allocator, as if by c(cont, alloc).

Overload resolution

This overload participates in overload resolution only if:

  • std::uses_allocator<Container, Alloc>::value is true (that is, if the underlying container is an allocator-aware container (true for all standard library containers that can be used with stack))

Example

#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);
}
Result (console)
H i ! 

Move container with allocator constructor

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

Constructs the underlying container with the contents of cont using move semantics while utilizing alloc as allocator, as if by c(std::move(cont), alloc).

Overload resolution

This overload participates in overload resolution only if:

  • std::uses_allocator<Container, Alloc>::value is true (that is, if the underlying container is an allocator-aware container (true for all standard library containers that can be used with stack))

Example

#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);
}
Result (console)
H i ! 

Copy stack with allocator constructor

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

Constructs the adaptor with the contents of other.c and using alloc as allocator, as if by c(other.c, alloc).

Overload resolution

This overload participates in overload resolution only if:

  • std::uses_allocator<Container, Alloc>::value is true (that is, if the underlying container is an allocator-aware container (true for all standard library containers that can be used with stack))

Example

#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);
}
Result (console)
H i ! 
H i !

Move stack with allocator constructor

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

Constructs the adaptor with the contents of other using move semantics while utilizing alloc as allocator, as if by c(std::move(other.c), alloc).

Overload resolution

This overload participates in overload resolution only if:

  • std::uses_allocator<Container, Alloc>::value is true (that is, if the underlying container is an allocator-aware container (true for all standard library containers that can be used with stack))

Example

#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);
}
Result (console)

H i !

Range based constructor with allocator (iterators)

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

Constructs the underlying container with the contents of the range [ first, last ) using alloc as allocator, as if by c(first, last, alloc).

Overload resolution

This overload participates in overload resolution only if:

Example

caution

This code can only be possibly compiled by a compiler that can support experimental C++23 mode.

#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);
}
Result (console)
!iH
H i !
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.