(C++17) | ||||
| Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
| Associative | ||||
| Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
| Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Views | ||||
(C++20) | ||||
(C++23) | ||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
std::stack| Member functions | ||||
| Element access | ||||
| Capacity | ||||
| Modifiers | ||||
(C++23) | ||||
(C++11) | ||||
stack::swap (C++11) | ||||
| Non-member functions | ||||
(C++11) | ||||
| Helper classes | ||||
(C++11) | ||||
(C++23) | ||||
| Deduction guides(C++17) |
void swap( stack& other)noexcept(/* see below */); | (since C++11) | |
other. Effectively callsusingstd::swap;swap(c, other.c);
Contents |
| other | - | container adaptor to exchange the contents with |
(none)
noexcept specification: noexcept(noexcept(swap(c, other.c))) In the expression above, the identifier | (since C++11) (until C++17) |
noexcept specification: noexcept(std::is_nothrow_swappable_v<Container>) | (since C++17) |
Same as underlying container (typically constant).
Some implementations (e.g. libc++) provide theswap member function as an extension to pre-C++11 modes.
#include <iostream>#include <concepts>#include <stack>#include <string>#include <string_view>#include <vector> template<typename Adaptor>requires(std::ranges::input_range<typename Adaptor::container_type>)void print(std::string_view name,const Adaptor& adaptor){struct Printer: Adaptor// to use protected Adaptor::Container c;{void print(std::string_view name)const{std::cout<< name<<" ["<<std::size(this->c)<<"]: ";for(autoconst& elem: this->c)std::cout<< elem<<' ';std::cout<<'\n';}}; static_cast<Printerconst&>(adaptor).print(name);} int main(){std::vector<std::string> v1{"1","2","3","4"}, v2{"Ɐ","B","Ɔ","D","Ǝ"}; std::stack s1(std::move(v1));std::stack s2(std::move(v2)); print("s1", s1); print("s2", s2); s1.swap(s2); print("s1", s1); print("s2", s2);}
Output:
s1 [4]: 4 3 2 1s2 [5]: Ǝ D Ɔ B Ɐs1 [5]: Ǝ D Ɔ B Ɐs2 [4]: 4 3 2 1
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2456 | C++11 | thenoexcept specification is ill-formed | made to work |
(C++11) | specializes thestd::swap algorithm (function template)[edit] |