다음을 통해 공유


basic_string::swap

두 문자열의 내용을 교환합니다.

void swap(
    basic_string<CharType, Traits, Allocator>& _Str
);

매개 변수

  • _Str
    The source string whose elements are to be exchanged with those in the destination string.

설명

If the strings being swapped have the same allocator object, the swap member function:

  • Occurs in constant time.

  • Throws no exceptions.

  • Invalidates no references, pointers, or iterators that designate elements in the two strings.

Otherwise, it performs a number of element assignments and constructor calls proportional to the number of elements in the two controlled sequences.

예제

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

int main( ) 
{
   using namespace std;

   // Declaring an objects 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;

   s1.swap ( s2 );
   cout << "After swapping string s1 and s2:" << endl;
   cout << " The basic_string s1 = " << s1 << "." << endl;
   cout << " The basic_string s2 = " << s2 << "." << endl;
}
  

요구 사항

헤더: <string>

네임스페이스: std

참고 항목

참조

basic_string 클래스