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 705595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:04:18+00:00 2026-05-14T04:04:18+00:00

I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead

  • 0

I recently learned that all stl containers have swap function:
i.e.

c1.swap(c2);  

will lead to object underlying c1 being assigned to c2 and vice versa.
I asked my professor if same is true in case of c1 and c2 being references.
he said same mechanism is followed.

I wonder how it happens since c++ references cannot be reseted.

  • 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-05-14T04:04:18+00:00Added an answer on May 14, 2026 at 4:04 am

    References are aliases. If you have two references, calling swap will swap what they are referring to, not the references themselves.

    C& r1 = c1; // r1 references c1
    C& r2 = c2; // r2 references c2
    
    r1.swap(r2); // same as c1.swap(c2)
    

    And it’s not the variables that get swapped, it’s what make them logically independent that gets swapped. If a container only consists of a pointer, if you swap that pointer with the pointer of another container, you’ve effectively swapped the containers. The variables themselves remain.


    Concrete example:

    typedef std::vector<int> vec;
    
    vec c1;
    vec c2;
    
    // fill em up
    
    c1.swap(c2);
    /*
    A vector, internally, has a pointer to a chunk of memory (and some other stuff).
    swap() is going to swap the internal pointer (and other stuff) with the other
    container. They are logically swapped.
    */
    
    vec& r1 = c1; // r1 is an alias for c1
    vec& r2 = c2; // r2 is an alias for c2
    
    r1.swap(r2); // same as c1.swap(c2). they are now unswapped
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently learned that sometimes people will lock specific processes or threads to specific
Possible Duplicate: Using A* to solve Travelling Salesman Problem I have recently learned that
I've recently learned that python doesn't have the switch/case statement. I've been reading about
From an old answer I have recently learned that Google Analytics, besides the traditional
I have recently learned the Ruby programming language, and all in all it is
I have recently learned that you can use a neat switch statement with fallthrough
I recently learned that table valued functions are not allowed with Entity Framework 4.1.
I have recently learned to create dynamic links via php, but my problem today
Recently I've learned that every computation cycle performs on machine words which on most
I started working in Python just recently and haven't fully learned all the nuts

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.