Namespaces
Variants

std::swap(std::basic_string)

cppreference.net에서 가져옴
 
 
 
std::basic_string
 
헤더 <string>
template< class CharT, class Traits, class Alloc >
void swap( std::basic_string<CharT, Traits, Alloc>& lhs,
           std::basic_string<CharT, Traits, Alloc>& rhs );
에 정의됨 (C++17까지)
template< class CharT, class Traits, class Alloc >
void swap( std::basic_string<CharT, Traits, Alloc>& lhs,
           std::basic_string<CharT, Traits, Alloc>& rhs ) noexcept(/* see below */);
(C++17부터)
(constexpr since C++20)

std::swap 알고리즘을 std::basic_string에 대해 특수화합니다. lhs과 rhs의 내용을 바꿉니다.

lhs.swap(rhs)와 동일합니다.

매개변수

lhs, rhs - 내용을 교환할 문자열들

반환값

(없음)

복잡도

상수.

예외

noexcept 명세:
noexcept ( noexcept ( lhs. swap ( rhs ) ) )
(C++17부터)

예제

#include <iostream>
#include <string>
int main()
{
    std::string a = "AAA";
    std::string b = "BBBB";
    std::cout << "Before swap:\n"
                 "a = " << a << "\n"
                 "b = " << b << "\n\n";
    std::swap(a, b);
    std::cout << "After swap:\n"
                 "a = " << a << "\n"
                 "b = " << b << '\n';
}

출력:

Before swap:
a = AAA
b = BBBB
After swap:
a = BBBB
b = AAA

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2064 C++11 비멤버 swap 이 noexcept이며 멤버 swap 과 일관되지 않음 noexcept 제거됨

참고 항목

내용을 교환합니다
(public member function)