basic_string_swap2
basic_string_swap2 (std::swap 전문화)
이 페이지는 std::basic_string에 대한 std::swap 알고리즘의 전문화(specialization)를 설명해요. 이 함수는 두 basic_string 객체의 내용을 교환하며, 멤버 함수 swap과 동일하게 동작해요. C++17부터 noexcept가 지정되고 C++20부터 constexpr로 사용할 수 있어요.
출처: cppreference
본문
| 헤더 |
||
|---|---|---|
| template < class CharT , class Traits , class Alloc > void swap ( std :: basic_string < CharT , Traits , Alloc >& lhs , std :: basic_string < CharT , Traits , Alloc >& rhs ); | (until 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 */ ); | (since C++17) (constexpr since C++20) |
std::basic_string에 대한 std::swap 알고리즘을 전문화해요. lhs와 rhs의 내용을 교환해요. lhs.swap(rhs)와 동등해요.
Parameters
| lhs, rhs | - | 내용을 교환할 문자열들이에요. |
Return value
(없음)
Complexity
상수 시간.
Exceptions
noexcept 명세: noexcept ( noexcept ( lhs . swap ( rhs ))) (since C++17)
Example
#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';
}
Output:
Before swap:
a = AAA
b = BBBB
After swap:
a = BBBB
b = AAA
Defect reports
| DR | 적용 대상 | 공개된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2064 | C++11 | non-member swap은 noexcept였으며 멤버 swap과 일관되지 않았어요 | noexcept 제거됨 |
See also
| swap | 내용을 교환한다 (public member function) [edit] |
|---|