std::stack::stack(3)
π₯ std::stack::stack(3)
Okay so this is the std::stack::stack page from the C++ Standard Libary manual β yes, the header spells it βLibary,β and your girl is keeping the lore intact β¨. This is all about constructing a std::stack, aka the container adaptor thatβs giving βstacked, literallyβ energy π
.
πΈ NAME πΈ
std::stack::stack β std::stack::stack (yes bestie, the name is the name, we love a self-titled icon π).
β¨ SYNOPSIS β¨
stack() : stack(Container()) {} (1) (since C++11)
explicit stack( const Container& cont = Container() (until C++11)
);
explicit stack( const Container& cont ); (2) (since C++11)
explicit stack( Container&& cont ); (3) (since C++11)
stack( const stack& other ); (4) (implicitly declared) (since C++11)
stack( stack&& other ); (5) (implicitly declared) (since C++11)
template< class InputIt > (6) (since C++23)
stack( InputIt first, InputIt last );
template< class Alloc > (7) (since C++11)
explicit stack( const Alloc& alloc );
template< class Alloc > (8) (since C++11)
stack( const Container& cont, const Alloc& alloc );
template< class Alloc > (9) (since C++11)
stack( Container&& cont, const Alloc& alloc );
template< class Alloc > (10) (since C++11)
stack( const stack& other, const Alloc& alloc );
template< class Alloc > (11) (since C++11)
stack( stack&& other, const Alloc& alloc );
template< class InputIt, class Alloc >
stack( InputIt first, InputIt last, const Alloc& (12) (since C++23)
alloc );
template< container-compatible-range<T> R> (13) (since C++23)
stack( std::from_range_t, R&& rg );
template< container-compatible-range<T> R, class Alloc > (14) (since C++23)
stack( std::from_range_t, R&& rg, const Alloc&
alloc );
π DESCRIPTION π
Constructs new underlying container of the container adaptor from a variety of data sources.
Okay so hereβs the breakdown, bestie β all 14 overloads, no skips, no cap:
- Default constructor. Value-initializes the container.
- Copy-constructs the underlying container
cwith the contents ofcont. This is also the default constructor. (until C++11) - Move-constructs the underlying container
cwithstd::move(cont). - Copy constructor. The adaptor is copy-constructed with the contents of
other.c. - Move constructor. The adaptor is constructed with
std::move(other.c). - Constructs the underlying container
cwith the contents of the range[first, last). This overload participates in overload resolution only ifInputItsatisfiesLegacyInputIterator.
Okay, before we hit 7 through 12: these constructors participate 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).
- Constructs the underlying container using
allocas allocator, as if byc(alloc). - Constructs the underlying container with the contents of
contand usingallocas allocator, as if byc(cont, alloc). - Constructs the underlying container with the contents of
contusing move semantics while utilizingallocas allocator, as if byc(std::move(cont), alloc). - Constructs the adaptor with the contents of
other.cand usingallocas allocator, as if byc(other.c, alloc). - Constructs the adaptor with the contents of
otherusing move semantics while utilizingallocas allocator, as if byc(std::move(other.c), alloc). - Constructs the underlying container with the contents of the range
[first, last)usingallocas allocator, as if byc(first, last, alloc). This overload participates in overload resolution only ifInputItsatisfiesLegacyInputIterator. - Constructs the underlying container with
ranges::to<Container>(std::forward<R>(rg)). - Constructs the underlying container with
ranges::to<Container>(std::forward<R>(rg), alloc).
π§ PARAMETERS π§
allocβ allocator to use for all memory allocations of the underlying container.otherβ another container adaptor to be used as source to initialize the underlying container.contβ container to be used as source to initialize the underlying container.first,lastβ range of elements[first, last)to initialize with.rgβ a container compatible range, that is, aninput_rangewhose elements are convertible toT.
π¦ TYPE REQUIREMENTS π¦
Allocmust meet the requirements ofAllocator.Containermust meet the requirements ofContainer. The constructors taking an allocator parameter participate in overload resolution only ifContainermeets the requirements ofAllocatorAwareContainer.InputItmust meet the requirements ofLegacyInputIterator.
β³ COMPLEXITY β³
Same as the corresponding operation on the wrapped container.
π NOTES π
Feature-test macro tea, served fresh:
__cpp_lib_adaptor_iterator_pair_constructorβ value202106L, std(C++23), feature: βIterator pair constructors forstd::queueandstd::stack; overloads (6) and (12)β.__cpp_lib_containers_rangesβ value202202L, std(C++23), feature: βRanges-aware construction and insertion; overloads (13) and (14)β.
π EXAMPLE π
// Run this code β literally, go run it, bestie:
#include <cassert>
#include <deque>
#include <iostream>
#include <memory>
#include <ranges>
#include <stack>
int main()
{
std::stack<int> c1;
c1.push(5);
assert(c1.size() == 1);
std::stack<int> c2(c1);
assert(c2.size() == 1);
std::deque<int> deq{3, 1, 4, 1, 5};
std::stack<int> c3(deq); // overload (2)
assert(c3.size() == 5);
# ifdef __cpp_lib_adaptor_iterator_pair_constructor
const auto il = {2, 7, 1, 8, 2};
std::stack<int> c4{il.begin(), il.end()}; // C++23, (6)
assert(c4.size() == 5);
# endif
# if __cpp_lib_containers_ranges >= 202202L
// C++23, overload (13)
auto c5 = std::stack(std::from_range_t, std::ranges::iota(0, 42));
assert(c5.size() == 42);
// the same effect with pipe syntax, internally uses overload (13)
auto c6 = std::ranges::iota(0, 42) | std::ranges::to<std::stack>();
assert(c6.size() == 42);
std::allocator<int> alloc;
// C++23, overload (14)
auto c7 = std::stack(std::from_range_t, std::ranges::iota(0, 42),
alloc);
assert(c7.size() == 42);
// the same effect with pipe syntax, internally uses overload (14)
auto c8 = std::ranges::iota(0, 42) |
std::ranges::to<std::stack>(alloc);
assert(c8.size() == 42);
# endif
}
π§ DEFECT REPORTS π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
P0935R0β applied to C++11. Behavior as published: βdefault constructor was explicit.β Correct behavior: βmade implicit.β
π SEE ALSO π
operator=β assigns values to the container adaptor (public member function).
mwah π β source/version: http://cppreference.com; date: 2024.06.10; page: std::stack::stack(3). Stay stacked, bestie ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms