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.
std::stack
is a class template, with following type parameters and member objects
that are used within constructors:
Type parameters
pub | T | The type of the stored elements. The behaviour is undefined ifT is not Container::value_type . (od C++17) |
pub | Container | The type of the underlying container used to store the elements. By default The container must satisfy SequenceContainer
The standard containers |
Member objects
prot | Container c | The underlying container. By default |
Default constructor
- od C++11
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() << ' ';
}
0 0
Copy constructor
- C++98
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);
}
Original data size: 5
Source size: 5
5 4 3 2 1
Sink size size: 5
5 4 3 2 1
Move constructor
- C++11
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);
}
Original data size: 5
Source size: 0
Sink size size: 5
5 4 3 2 1
Copy container constructor
- C++11
explicit stack( const Container& cont );
Copy-constructs the underlying container c
with the contents of cont
. This is also the default constructor. (do 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);
}
Original data size: 5
5 5 4 3 2 1
Move container constructor
- C++11
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);
}
Original data size: 0
5 5 4 3 2 1
Range based constructor (iterators)
- C++23
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:
InputIt
satisfiesLegacyInputIterator
Complexity
Constant - O(1).
Example
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);
}
5 4 3 2 1
Default construct underlying container with allocator constructor
- C++11
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
istrue
(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);
}
H i !
Copy container with allocator constructor
- C++11
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
istrue
(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);
}
H i !
Move container with allocator constructor
- C++11
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
istrue
(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);
}
H i !
Copy stack with allocator constructor
- C++11
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
istrue
(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);
}
H i !
H i !
Move stack with allocator constructor
- C++11
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
istrue
(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);
}
H i !
Range based constructor with allocator (iterators)
- C++23
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:
InputIt
satisfiesLegacyInputIterator
Example
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);
}
!iH
H i !
Hover to see the original license.