std::map::swap(3) C++ Standard Libary std::map::swap(3) NAME std::map::swap - std::map::swap Synopsis void swap( map& other ); (until C++17) void swap( map& other ) noexcept(/* see below */); (since C++17) Exchanges the contents of the container with those of other. Does not invoke any move, copy, or swap operations on individual elements. All iterators and references remain valid. The end() iterator is invali- dated. The Compare objects must be Swappable, and they are exchanged using un- qualified call to non-member swap. If std::allocator_traits<allocator_type>::propagate_on_con- tainer_swap::value is true, then the allocators are exchanged using an unqualified call to (since non-member swap. Otherwise, they are not swapped (and if get_allocator() != C++11) other.get_allocator(), the behavior is undefined). Parameters other - container to exchange the contents with Return value (none) Exceptions Any exception thrown by the swap of the Compare objects. (until C++17) noexcept specification: noexcept(std::allocator_traits<Allocator>::is_always_equal::value (since C++17) && std::is_nothrow_swappable<Compare>::value) Complexity Constant. Example // Run this code #include <iostream> #include <string> #include <utility> #include <map> // print out a std::pair template<class Os, class U, class V> Os& operator<<(Os& os, const std::pair<U, V>& p) { return os << p.first << ':' << p.second; } // print out a container template<class Os, class Co> Os& operator<<(Os& os, const Co& co) { os << '{'; for (auto const& i : co) os << ' ' << i; return os << " }\n"; } int main() { std::map<std::string, std::string> m1{{"I^3", "gamma"}, {"I^2", "beta"}, {"I+/-", "alpha"}, {"I^3", "gamma"}}, m2{{"I<micro>", "epsilon"}, {"I'", "delta"}, {"I<micro>", "ep- silon"}}; const auto& ref = *(m1.begin()); const auto iter = std::next(m1.cbegin()); std::cout << "aaaaaaaa before swap aaaaaaaa\n" << "m1: " << m1 << "m2: " << m2 << "ref: " << ref << "\niter: " << *iter << '\n'; m1.swap(m2); std::cout << "aaaaaaaa after swap aaaaaaaa\n" << "m1: " << m1 << "m2: " << m2 << "ref: " << ref << "\niter: " << *iter << '\n'; // Note that every iterator referring to an element in one container before // the swap refers to the same element in the other container after the swap. // Same is true for references. } Output: aaaaaaaa before swap aaaaaaaa m1: { I+/-:alpha I^2:beta I^3:gamma } m2: { I':delta I<micro>:epsilon } ref: I+/-:alpha iter: I^2:beta aaaaaaaa after swap aaaaaaaa m1: { I':delta I<micro>:epsilon } m2: { I+/-:alpha I^2:beta I^3:gamma } ref: I+/-:alpha iter: I^2:beta See also std::swap(std::map) specializes the std::swap algorithm (function template) Category: * conditionally noexcept http://cppreference.com 2024.06.10 std::map::swap(3)
NAME | Synopsis | Parameters | Return value | Exceptions | Complexity | Example | Output: | See also | Category:
Want to link to this manual page? Use this URL:
<https://man.FreeBSD.org/cgi/man.cgi?query=std::map::swap&sektion=3&manpath=FreeBSD+Ports+15.1.quarterly>