std::swap(std::vector)(3)

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

πŸ’– std::swap(std::vector)(3) πŸ’–

okay so bestie, this page is from the C++ Standard Libary manual (yes, that's the manual name, spelling and all πŸ’…), and it's all about the std::swap glow-up for std::vector. no cap, it's a specialization and it's giving "two vectors trade lives" energy. ✨


🌸 NAME 🌸

  • std::swap(std::vector) - std::swap(std::vector). that's the whole name, bestie. it's literally the std::swap(std::vector) specialization. 🫢

✨ Synopsis ✨

here's the signature tea, exactly as it appears β€” lowkey time-travel vibes with (until C++17) and (since C++17):

Defined in header <vector>
template< class T, class Alloc >

void swap( std::vector<T, Alloc>& lhs,     (until C++17)

           std::vector<T, Alloc>& rhs );
template< class T, class Alloc >

void swap( std::vector<T, Alloc>& lhs,     (since C++17)
           std::vector<T, Alloc>& rhs )    (constexpr since C++20)

                         noexcept(/* see below */);

so it specializes the std::swap algorithm for std::vector. it swaps the contents of lhs and rhs. it calls lhs.swap(rhs). periodt. πŸ’…

🫢 Parameters 🫢

  • lhs, rhs β€” containers whose contents to swap.

πŸŽ€ Return value πŸŽ€

  • (none) β€” literally no return value, bestie. 😌

⚑ Complexity ⚑

Constant. it's constant, no stress. πŸ”₯

😭 Exceptions 😭

noexcept specification: (since C++17) β€” noexcept(noexcept(lhs.swap(rhs))).

🌈 Example 🌈

run this code, it's iconic β€” ngl, this example ate:

// Run this code

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> alice{1, 2, 3};
    std::vector<int> bob{7, 8, 9, 10};

    auto print = [](const int& n) { std::cout << ' ' << n; };

    // Print state before swap
    std::cout << "Alice:";
    std::for_each(alice.begin(), alice.end(), print);
    std::cout << "\n" "Bob  :";
    std::for_each(bob.begin(), bob.end(), print);
    std::cout << '\n';

    std::cout << "-- SWAP\n";
    std::swap(alice, bob);

    // Print state after swap
    std::cout << "Alice:";
    std::for_each(alice.begin(), alice.end(), print);
    std::cout << "\n" "Bob  :";
    std::for_each(bob.begin(), bob.end(), print);
    std::cout << '\n';
}

πŸ“€ Output: πŸ“€

Alice: 1 2 3
Bob  : 7 8 9 10
-- SWAP
Alice: 7 8 9 10
Bob  : 1 2 3

πŸ‘€ See also πŸ‘€

  • swap β€” swaps the contents (public member function).

🏷️ Category 🏷️

  • conditionally noexcept

okay that's the whole page, bestie. source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::swap(std::vector)(3). mwah πŸ’‹
πŸ“¦ where this page comes from