cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Strings library : string : swap
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Strings library
char_traits
classes:
· string
global functions:
· getline
· operator+
· operator<<
· operator>>
· comparison operators
· swap
string
· string::string
member constants:
· string::npos
member functions:
· string::append
· string::assign
· string::at
· string::begin
· string::capacity
· string::clear
· string::compare
· string::copy
· string::c_str
· string::data
· string::empty
· string::end
· string::erase
· string::find
· string::find_first_not_of
· string::find_first_of
· string::find_last_not_of
· string::find_last_of
· string::get_allocator
· string::insert
· string::length
· string::max_size
· string::operator+=
· string::operator=
· string::operator[]
· string::push_back
· string::rbegin
· string::rend
· string::replace
· string::reserve
· string::resize
· string::rfind
· string::size
· string::substr
· string::swap

-

string::swap public member function
void swap ( string& str );

Swap contents with another string

Swaps the contents of the string with those of string object str, such that after the call to this member function, the contents of this string are those which were in str before the call, and the contents of str are those which were in this string.

Notice that a global function with the same name, swap, exists with the same behavior, and which acts as a specialization for strings of the algorithm function with the same name.

Parameters

str
a string object to swap its contents with those of the object.

Return value

none

Example

// swap strings
#include <iostream>
#include <string>
using namespace std;

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

  cout << "Before swap, buyer has " << buyer;
  cout << " and seller has " << seller << endl;

  seller.swap (buyer);

  cout << " After swap, buyer has " << buyer;
  cout << " and seller has " << seller << endl;

  return 0;
}

Output:

Before swap, buyer has money and seller has goods
After swap, buyer has goods and seller has money

Basic template member declaration

( basic_string<charT,traits,Allocator> )
void swap ( basic_string& str );

See also

swap Swap contents of two strings (function)
string::replace Replace part of string (public member function)
string::assign Assign content to string (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us