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

FreeBSD Manual Pages

  
 
  

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

NAME
       std::basic_string::swap - std::basic_string::swap

Synopsis
	  void		swap(	       basic_string&	      other	    );
       (until C++17)
	  void	swap(  basic_string&  other  )	noexcept(/*  see  below	  */);
       (since C++17)
										 (un-
       til C++20)
	  constexpr  void  swap(  basic_string&	 other ) noexcept(/* see below
       */);  (since C++20)

	  Exchanges the	contents of the	string with those of other. All	itera-
       tors and
	  references may be invalidated.

	  The behavior is undefined if Allocator does not  propagate  on  swap
       and  (since C++11)
	  the allocators of *this and other are	unequal.

Parameters
	  other	- string to exchange the contents with

Return value
	  (none)

Complexity
	  Constant.

Exceptions
											(since
	  noexcept						specification:
       C++17)
	  noexcept(std::allocator_traits<Allocator>::propagate_on_con-
       tainer_swap::value
	  || std::allocator_traits<Allocator>::is_always_equal::value)

Example
       // Run this code

	#include <string>
	#include <iostream>

	int main()
	{
	    std::string	a = "AAA";
	    std::string	b = "BBB";

	    std::cout << "before swap" << '\n';
	    std::cout << "a: " << a << '\n';
	    std::cout << "b: " << b << '\n';

	    a.swap(b);

	    std::cout << "after	swap" << '\n';
	    std::cout << "a: " << a << '\n';
	    std::cout << "b: " << b << '\n';
	}

Output:
	before swap
	a: AAA
	b: BBB
	after swap
	a: BBB
	b: AAA

See also
	  swap	      swaps the	values of two objects
		      (function	template)
	  swap_ranges swaps two	ranges of elements
		      (function	template)
	  swap	      swaps the	contents
	  (C++17)	  (public     member	 function     of      std::ba-
       sic_string_view<CharT,Traits>)

http://cppreference.com		  2022.07.31	    std::basic_string::swap(3)

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

home | help