std::basic_..._view::swap(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

okay bestie, welcome to the std::basic_..._view::swap page from the C++ Standard Libary manual (yes, that’s the manual’s spelling and we’re not fixing it πŸ’…). The NAME line is doing that iconic double-repeat thing: std::basic_string_view::swap β€” std::basic_string_view::swap. slay, we love a consistent queen ✨.

✨ Synopsis ✨

constexpr void swap( basic_string_view& v ) noexcept;

and yes, it’s (since C++17) β€” been serving looks since then 🫢.

It exchanges the view with that of v. Like, the views trade places. That’s the whole gig, no cap.

πŸŽ€ Parameters πŸŽ€

  • v β€” view to swap with. that’s it, that’s the parameter πŸ’–

🌸 Return value 🌸

(none) β€” literally nothing comes back. void behavior, periodt.

πŸ¦‹ Complexity πŸ¦‹

Constant. no scaling drama, no surprise plot twist, just the note β€œConstant.” and we move on πŸ’….

πŸ”₯ Example πŸ”₯

// Run this code

#include <iostream>
#include <string_view>

int main()
{
    std::string_view a = "AAA";
    std::string_view b = "BBBB";

    std::cout << "Before swap:\n"
                 "a = " << a << "\n"
                 "b = " << b << "\n\n";

    a.swap(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

🌈 See also 🌈

  • swap β€” swaps the values of two objects (function template)
  • swap_ranges β€” swaps two ranges of elements (function template)
  • swap β€” public member function of std::basic_string<CharT,Traits,Allocator>; the note next to it says swaps the contents 🫢

source/version: http://cppreference.com Β· dated 2024.06.10 Β· that was the std::basic_..._view::swap(3) page, mwah πŸ’‹

πŸ“¦ where this page comes from