C++ string swap() Function
Example
Exchange the content of two strings:
string firstName = "John";
string lastName = "Doe";
firstName.swap(lastName);
cout << firstName << "\n";
cout << lastName;
Definition and Usage
The swap() function exchanges the content of two strings. After the swap, the first string has the content of the second string, and the second string has the content of the first.
The swap function is defined in the <string> header file.
Syntax
string.swap(other);
Parameter Values
| Parameter | Description |
|---|---|
| other | Required. The string to exchange content with. |
Technical Details
| Returns: | Nothing (void). |
|---|
Related Pages
Read more about strings in our String Tutorial.