std::swap(std::valarray)(3)
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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms