std::swap(std::valarray)(3)

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

std::swap(std::valarray)(3)

okay so bestie, this page is from the C++ Standard Libary manual, and we're talking about std::swap(std::valarray) β€” the valarray swap glow-up. no cap, it's short but iconic.

πŸ’– NAME πŸ’–

std::swap(std::valarray) - std::swap(std::valarray)

πŸŽ€ Synopsis πŸŽ€

Defined in header <valarray>

template< class T >
(since C++11)
void swap( std::valarray<T>& lhs, std::valarray<T>& rhs ) noexcept;

so what's the tea? it specializes the std::swap algorithm for std::valarray. it swaps the contents of lhs and rhs, and it calls lhs.swap(rhs). that's the whole move, periodt. πŸ’…

🌸 Parameters 🌸

  • lhs, rhs β€” valarrays whose contents to swap. yes, both of them, bestie.

πŸ’« Return value πŸ’«

(none) β€” literally nothing comes back. it's giving void energy, and that's on purpose. 🫢

⚑ Complexity ⚑

Constant. that's it, that's the complexity, no dramatic plot twist. ✨

🧚 Example 🧚

// Run this code

#include <iostream>
#include <valarray>

void print(auto rem, const std::valarray<int>& v)
{
    std::cout << rem << '{';
    for (char sep[]{0, ' ', 0}; auto elem : v)
        std::cout << sep << elem, *sep = ',';
    std::cout << "}\n";
}

int main()
{
    std::valarray x{3, 1, 4, 1, 5};
    std::valarray y{2, 7, 1, 8};

    print("Before swap:\n" "x: ", x);
    print("y: ", y);

    std::swap(x, y);

    print("After swap:\n" "x: ", x);
    print("y: ", y);
}

πŸ“€ Output πŸ“€

Before swap:
x: {3, 1, 4, 1, 5}
y: {2, 7, 1, 8}
After swap:
x: {2, 7, 1, 8}
y: {3, 1, 4, 1, 5}

πŸ‘€ See also πŸ‘€

  • swap β€” swaps with another valarray (public member function). lowkey useful, highkey same vibe. 🌈

sign-off for the footer vibe: source/version is http://cppreference.com, dated 2024.06.10 β€” mwah, stay iconic πŸ’‹βœ¨

πŸ“¦ where this page comes from