Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8490801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:13:53+00:00 2026-06-10T22:13:53+00:00

I have three questions regarding swapping, most likely pretty basic to you. (1) With

  • 0

I have three questions regarding swapping, most likely pretty basic to you.

(1) With regards to two same type STL containers a and b, both operations below would work

swap(a,b);
a.swap(b);

I understand that the second is specialized for the container (e.g., only involves a number of iterator swaps) while the first is a global algorithm meant to work with generic datatypes and performs an internal copy-construct.

My question is if I write the first, will the compiler use the second regardless, or do I have to be careful to check if an appropriate specialization exists?

(2) Would swap(a,b) result in the same performance with swap(b,a)? If a copy-construct is involved and the objects are of considerably different size, I suspect it may matter?

(3) In cases where the overloaded operator == exists and is relatively fast, checking for a == b before swapping would make sense so as to avoid unnecessary operations. Does std::swap apply this check first, or does it perform the operation regardless?

Thanks for your time!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:13:55+00:00Added an answer on June 10, 2026 at 10:13 pm
    1. The global swap template is specialized for each of the standard library types that supports swap; the specialization calls the member swap.

    2. There should be no difference between swap(a, b) and swap(b, a). (I can imagine perverse types where it would matter, but that would never occur in practice).

    3. No, std::swap typically doesn’t check for equality. In many cases that would be slower than just doing the swap.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three questions regarding a homework assignment for C++. The goal was to
I have two questions regarding python libraries: I would like to know if there
I have two questions regarding jsTree: I am generating a jsTree using JSON with
I have two questions regarding coding Haskell in Emacs/VIM in Linux : If one
I have two questions regarding the sl4a scripting language. I'd like to develop a
Recently there have been a couple questions regarding static type constraints and inline: Use
I have two questions regarding Java web application deployment and its impact on performance.
There have been some similar questions asked regarding Grid views, but none have been
I have a few questions regarding JavaScript obfuscation on client side. First question :
There have been other questions regarding the subject of verifying the accessibility and accessibility

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.