Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

swap (<string>)

Exchanges the arrays of characters of two strings.

template<class Traits, class Allocator> 
   void swap( 
      basic_string<CharType, Traits, Allocator>& _Left, 
      basic_string<CharType, Traits, Allocator>& _Right 
   );
_Left

One string whose elements are to be swapped with those of another string.

_Right

The other string whose elements are to be swapped with the first string.

The template function executes the specialized member function _Left.swap(_Right) for strings, which guarantees constant complexity.

// string_swap.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( ) 
{
   using namespace std;
   // Declaring an object of type basic_string<char>
   string s1 ( "Tweedledee" );
   string s2 ( "Tweedledum" );
   cout << "Before swapping string s1 and s2:" << endl;
   cout << "The basic_string s1 = " << s1 << "." << endl;
   cout << "The basic_string s2 = " << s2 << "." << endl;

   swap ( s1 , s2 );
   cout << "\nAfter swapping string s1 and s2:" << endl;
   cout << "The basic_string s1 = " << s1 << "." << endl;
   cout << "The basic_string s2 = " << s2 << "." << endl;
}
Before swapping string s1 and s2:
The basic_string s1 = Tweedledee.
The basic_string s2 = Tweedledum.

After swapping string s1 and s2:
The basic_string s1 = Tweedledum.
The basic_string s2 = Tweedledee.

Header: <string>

Namespace: std

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.