std::swap
The function std::swap is a utility in the C++ Standard Library that exchanges the values of two variables. It takes two arguments by reference and swaps their contents, allowing for efficient value exchange without needing a temporary variable. This function is particularly useful in algorithms that require rearranging elements, such as sorting.
Using std::swap is straightforward; simply call it with the two variables you want to swap. For example, if you have two integers, a and b, calling std::swap(a, b) will interchange their values. This function is also optimized for user-defined types, making it versatile for various data structures.