A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/string/string/swap-free/ below:

function

<string>

std::swap (string)
void swap (string& x, string& y);

Exchanges the values of two strings

Exchanges the values of string objects x and y, such that after the call to this function, the value of x is the one which was on y before the call, and the value of y is that of x.

This is an overload of the generic algorithm swap that improves its performance by mutually transferring ownership over their internal data to the other object (i.e., the strings exchange references to their data, without actually copying the characters): It behaves as if x.swap(y) was called.



Parameters
x,y
string objects to swap.

Return value none

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// swap strings
#include <iostream>
#include <string>

main ()
{
  std::string buyer ("money");
  std::string seller ("goods");

  std::cout << "Before the swap, buyer has " << buyer;
  std::cout << " and seller has " << seller << '\n';

  swap (buyer,seller);

  std::cout << " After the swap, buyer has " << buyer;
  std::cout << " and seller has " << seller << '\n';

  return 0;
}

Output:
Before the swap, buyer has money and seller has goods
 After the swap, buyer has goods and seller has money


Complexity Constant.

Iterator validity Any iterators, pointers and references related to both x and y may be invalidated.

Data races Both objects, x and y, are modified.

Exception safetyNo-throw guarantee: this member function never throws exceptions.

See also
string::swap
Swap string values (public member function)
swap
Exchange values of two objects (function template)
swap_ranges
Exchange values of two ranges (function template)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4