std::swap(std::vector)(3)
π 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 thestd::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
- 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