FreeBSD Manual Pages
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 past-the-end iterator is invalidated. The Compare objects must be Swappable, and they are exchanged using unqualified 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_alloca- tor() != 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 { {"", "gamma"}, {"", "beta"}, {"", "alpha"}, {"", "gamma"}, }, m2 { {"", "epsilon"}, {"", "delta"}, {"", "epsilon"} }; const auto& ref = *(m1.begin()); const auto iter = std::next(m1.cbegin()); std::cout << " before swap \n" << "m1: " << m1 << "m2: " << m2 << "ref: " << ref << "\niter: " << *iter << '\n'; m1.swap(m2); std::cout << " after swap \n" << "m1: " << m1 << "m2: " << m2 << "ref: " << ref << "\niter: " << *iter << '\n'; // Note that every iterator referring to an element in one con- tainer before // the swap refers to the same element in the other container af- ter the swap. // Same is true for references. } Output: before swap m1: { :alpha :beta :gamma } m2: { :delta :epsilon } ref: :alpha iter: :beta after swap m1: { :delta :epsilon } m2: { :alpha :beta :gamma } ref: :alpha iter: :beta See also std::swap(std::map) specializes the std::swap algorithm (function template) http://cppreference.com 2022.07.31 std::map::swap(3)
NAME | Synopsis | Parameters | Return value | Exceptions | Complexity | Example | Output: | See also
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.0>
