Javatpoint Logo
Javatpoint Logo

Difference between std::swap and std::vector::swap

in C++. But before discussing the difference, we must know about the std::swap and std::vector::swap in C++.

What is std::swap?

The utility function std::swap is defined in the C++ standard library's <algorithm> header. It allows swapping the values of two objects.

Syntax:

The syntax of std::swap is:

It takes two arguments; both reference objects of type T. It swaps the values using move semantics or copy and assignment.

Some key points about std::swap:

  • It is templated, so it works for any type that supports copy/move
  • It efficiently swaps values by moving rather than copying.
  • Many C++ algorithms and containers use it to shuffle elements around.
  • Specializations for some types like std::string and std::vector are provided to optimize performance.
  • It provides a consistent way to swap objects without needing to rewrite swap code everywhere.

Some examples of using std::swap:

Example:

Let us take an example code in C++ that illustrates swapping two vectors using the std::swap() function.

Output:

Original Vector 1: 1 2 3 4 5
Original Vector 2: 10 20 30 40 50
Vectors after swapping:
Vector 1 (after swap): 10 20 30 40 50
Vector 2 (after swap): 1 2 3 4 5

Explanation:

In this example, the code defines two vectors, displays their original content, swaps them using std::swap(), and then displays the content of the vectors after the swap. Note that std::swap() is used to change all the vectors efficiently.

What is std::vector::swap?

The std::vector::swap member function of the std::vector class efficiently swaps the contents of one vector with another.

Syntax:

It has the following syntax:

It takes one argument - a reference to another std::vector to swap contents with.

Some key points about std::vector::swap:

  • It swaps the contents by changing the internal pointers, sizes, capacities, etc. It makes it very efficient, as no actual data movement is required.
  • The vectors must be of the same type (holding the same data type). Their current allocations can be different.
  • It does not invalidate any references, pointers, and iterators referring to the elements in the vectors. The details are still the same; only their order has changed.
  • It runs in constant O(1) time as it swaps internal members.

Example usage:

Example:

The following C++ code demonstrates how to swap two vectors using std::vector::swap().

Output:

Original Vector 1: 1 2 3 4 5
Original Vector 2: 10 20 30 40 50
Vectors after swapping:
Vector 1 (after swap): 10 20 30 4 5
Vector 2 (after swap): 1 2 3 40 50

Explanation:

Just like the previous example, the content of Vector 1 has been swapped with the range of Vector 2 using std::vector::swap().

Major key differences between std::swap and std::vector::swap

Difference between std::swap and std::vector::swap

Here is a table summarizing the key differences between std::swap and std::vector::swap:

Feature std::swap std::vector::swap
Defined in <algorithm> header <vector> header within std::vector class
What it swaps Swaps values of any two objects Swaps contents of two std::vector objects
Arguments Takes references to two objects as arguments Takes a reference to another vector as an argument
Generality Works for any type with basic swap support Works specifically for std::vector objects
Efficiency It may require copying or moving element data. Swaps vector internal pointers - no data movement
Speed Depends on types; string/vector specializations optimize it. Constant time O(1) - very fast
Impact on iterators Invalidates iterators into arguments Does not invalidate any iterators or pointers
Internal mechanics Copies or moves underlying objects Manipulates internal metadata of vectors
Usage Used generally with any swappable objects Used to efficiently shuffle vector contents
Example usage std::swap(v1, v2); v1.swap(v2);

In summary, std::swap works for generic object swapping, while std::vector::swap leverages the vector's internal structure for an optimized, speedy vector shuffle, avoiding reallocation. The latter does not break any existing references into the vectors.

Conclusion:

In conclusion, std::swap exchanges any two objects by utilizing copy/move operations, while vector::swap leverages the vector's internal structure to efficiently shuffle contents in constant O(1) time without invalidating iterators.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA