Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::unordered_map::swap(3)   C++ Standard Libary  std::unordered_map::swap(3)

NAME
       std::unordered_map::swap	- std::unordered_map::swap

Synopsis
	  void swap( unordered_map& other );				(since
       C++11)
									(until
       C++17)
	  void swap( unordered_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 in-
       validated.

	  The Hash and KeyEqual	objects	must be	Swappable, and	they  are  ex-
       changed using
	  unqualified calls 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 Hash	or  KeyEqual  objects.
       (until C++17)
	  noexcept specification:
	  noexcept(std::allocator_traits<Allocator>::is_always_equal::value
									    (since
       C++17)
	  && std::is_nothrow_swappable<Hash>::value

	  && std::is_nothrow_swappable<key_equal>::value)

Complexity
	  Constant.

Example
       // Run this code

	#include <iostream>
	#include <string>
	#include <utility>
	#include <unordered_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::unordered_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.
	}

Possible 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::unordered_map)	specializes the	std::swap algorithm
	  (C++11)			(function template)

Category:
	    * conditionally noexcept

http://cppreference.com		  2024.06.10	   std::unordered_map::swap(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::unordered_map::swap&sektion=3&manpath=FreeBSD+Ports+15.1.quarterly>

home | help