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

  • Home
  • SEARCH
  • 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 3336486
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:08:29+00:00 2026-05-18T00:08:29+00:00

If I swap two vectors, will their iterators remain valid, now just pointing to

  • 0

If I swap two vectors, will their iterators remain valid, now just pointing to the “other” container, or will the iterator be invalidated?

That is, given:

using namespace std;
vector<int> x(42, 42);
vector<int> y;
vector<int>::iterator a = x.begin(); 
vector<int>::iterator b = x.end();

x.swap(y);

// a and b still valid? Pointing to x or y?

It seems the std mentions nothing about this:

[n3092 – 23.3.6.2]

void swap(vector<T,Allocator>& x);

Effects:
Exchanges the contents and capacity()
of *this with that of x.

Note that since I’m on VS 2005 I’m also interested in the effects of iterator debug checks etc. (_SECURE_SCL)

  • 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-18T00:08:29+00:00Added an answer on May 18, 2026 at 12:08 am

    The behavior of swap has been clarified considerably in C++11, in large part to permit the Standard Library algorithms to use argument dependent lookup (ADL) to find swap functions for user-defined types. C++11 adds a swappable concept (C++11 §17.6.3.2[swappable.requirements]) to make this legal (and required).

    The text in the C++11 language standard that addresses your question is the following text from the container requirements (§23.2.1[container.requirements.general]/8), which defines the behavior of the swap member function of a container:

    Every iterator referring to an element in one container before the swap shall refer to the same element in the other container after the swap.

    It is unspecified whether an iterator with value a.end() before the swap will have value b.end() after the swap.

    In your example, a is guaranteed to be valid after the swap, but b is not because it is an end iterator. The reason end iterators are not guaranteed to be valid is explained in a note at §23.2.1/10:

    [Note: the end() iterator does not refer to any element, so it may be
    invalidated. –end note]

    This is the same behavior that is defined in C++03, just substantially clarified. The original language from C++03 is at C++03 §23.1/10:

    no swap() function invalidates any references, pointers, or iterators referring to the elements of the containers being swapped.

    It’s not immediately obvious in the original text, but the phrase “to the elements of the containers” is extremely important, because end() iterators do not point to elements.

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

Sidebar

Related Questions

I am trying to 'swap' two cells' contents, and their mappings. To do this,
I am trying to swap two LinearLayouts by setting their visibility properties to VISIBLE
Just wondering if anyone knows the keyboard shortcut to swap around two sides of
Is there a Common Lisp function that will swap two elements in a list
I want to swap two integers, and I want to know which of these
I know that ASP.NET MVC will allow me to swap in various View engines
I'm trying to swap two variables in ActionScript. I tried using: a = 42
how to swap two numbers inplace without using any additional space?
I'm trying to swap two strings but I'm not sure if what I am
I want to swap two xml elements. How do i do this? Here is

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.