cpp / latest / string / basic_string_view / swap.html /

std::basic_string_view<CharT,Traits>::swap

constexpr void swap( basic_string_view& v ) noexcept;
(since C++17)

Exchanges the view with that of v.

Parameters

v - view to swap with

Return value

(none).

Complexity

Constant.

Example

#include <string_view>
#include <iostream>
 
int main() {
    auto s1{ std::string_view{"⏺⏺⏺⏺⏺"} };
    auto s2{ std::string_view{"⏹⏹⏹⏹⏹"} };
 
    std::cout << "Before : " << s1 << ' ' << s2 << "\n";
    s1.swap(s2);
    std::cout << "After  : " << s1 << ' ' << s2 << "\n";
}

Output:

Before : ⏺⏺⏺⏺⏺ ⏹⏹⏹⏹⏹
After  : ⏹⏹⏹⏹⏹ ⏺⏺⏺⏺⏺

See also

swaps the values of two objects
(function template)
swaps two ranges of elements
(function template)
swaps the contents
(public member function of std::basic_string<CharT,Traits,Allocator>)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/string/basic_string_view/swap