std::swap(s...rward_list)(3)
π std::swap(std::forward_list) β the C++ Standard Library glow-up, section 3 π¦
Okay so bestie, we're in the C++ Standard Libary manual, section 3, and today's main character is std::swap(std::forward_list). Yes, the title is literally std::swap(std::forward_list) twice, she said "I contain multitudes." Periodt.
β¨ NAME β¨
std::swap(std::forward_list) β which, and I cannot stress this enough, is std::swap(std::forward_list). Iconic. No notes. π
πΈ SYNOPSIS πΈ
She lives in the header <forward_list> (say it with me: Defined in header <forward_list>), and here's her whole lewk:
Defined in header <forward_list>
template< class T, class Alloc >
void swap( std::forward_list<T, Alloc>& lhs, (since C++11)
std::forward_list<T, Alloc>& rhs ); (until C++17)
template< class T, class Alloc >
void swap( std::forward_list<T, Alloc>& lhs, (since C++17)
std::forward_list<T, Alloc>& rhs )
noexcept(/* see below */);
So like β (since C++11) she's been here, and (until C++17) that first form was the vibe, then (since C++17) she got the noexcept(/* see below */) glow-up. Growth. We love to see it. π
π«Ά DESCRIPTION π«Ά
So what does she do? She Specializes the std::swap algorithm for std::forward_list. That's the whole assignment, lowkey.
Then: Swaps the contents of lhs and rhs. Simple. Elegant. Two lists walk in, two lists walk out with each other's stuff.
And the actual mechanics? Calls lhs.swap(rhs). That's it. She's not doing the heavy lifting herself, she's delegating β main character energy with good management skills, honestly. π
π PARAMETERS π
lhs, rhsβ thecontainers whose contents to swap. Two besties trading outfits. That's the whole bit. π
β RETURN VALUE β
(none)
Nothing. Nada. She gives you the swap and dips. No cap, she's just that generous. π
π§ COMPLEXITY π§
Constant.
Constant!! Not linear, not "depends," just constant. O(1) behavior from an icon who respects your time. We stan an efficient queen. β‘
π₯ EXCEPTIONS π₯
Here's the noexcept specification: β and this is (since C++17), so mark your calendars:
noexcept(noexcept(lhs.swap(rhs)))
Gorgeous. She's only noexcept when lhs.swap(rhs) is noexcept. Conditional. Situational. She reads the room. π
π EXAMPLE π
// Run this code β okay bestie, here's the full thing, typed out exactly, don't @ me:
#include <algorithm>
#include <iostream>
#include <forward_list>
int main()
{
std::forward_list<int> alice{1, 2, 3};
std::forward_list<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';
}
Alice and Bob. The classic duo. Not me getting emotionally invested in two std::forward_list<int>s. π
π¦ Output π¦
Alice: 1 2 3
Bob : 7 8 9 10
-- SWAP
Alice: 7 8 9 10
Bob : 1 2 3
THEY SWAPPED. Look at them. Alice took Bob's 7 8 9 10 and Bob got the 1 2 3 and everybody's thriving. The way that -- SWAP just sits there being a whole diva divider. Ate. π₯
π SEE ALSO π
swapβ swaps the contents. Yeah. That's the description.(public member function). She's the member-function version and she also just... swaps the contents. Two queens, same energy, different scopes. Slay. π
π CATEGORY π
* conditionally noexcept
Which is exactly the vibe we established in EXCEPTIONS. Consistent. Reliable. A little bit conditional. Like all of us. π§
Ngl bestie, this one's genuinely simple: you callstd::swap(alice, bob), it callslhs.swap(rhs), both are constant time, and thenoexcept-ness rides entirely on whateverlhs.swap(rhs)says. No hidden lore, no traps. Just two lists getting a whole new wardrobe. π
Fr fr, this page came from cppreference.com, dated 2024.06.10, and it's std::swap(s...rward_list)(3) β go forth and swap, bestie. π«Άβ¨
π¦ 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