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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:31:56+00:00 2026-05-21T23:31:56+00:00

What is the behavior of std::advance when you have say: std::vector<int> foo(10,10); auto i

  • 0

What is the behavior of std::advance when you have say:

std::vector<int> foo(10,10);
auto i = foo.begin();
std::advance(i, 20);

What is the value of i? Is it foo.end()?

  • 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-21T23:31:57+00:00Added an answer on May 21, 2026 at 11:31 pm

    You are passing the foo size by advancing to 20th position. Definitely it is not end of the vector. It should invoke undefined behavior on dereferencing, AFAIK.

    Edit 1:

    #include <algorithm>
    #include <vector>
    #include <iostream>
    
    int main()
    {
         std::vector<int> foo(10,10) ;
         std::vector<int>::iterator iter = foo.begin() ;
         std::advance(iter,20);
    
         std::cout << *iter << "\n" ;
    
         return 0;
    }
    

    Output: 0

    If it is the vector’s last element, then it should have given 10 on iterator dereferencing. So, it is UB.

    IdeOne Results

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

Sidebar

Related Questions

I know that compilers have much freedom in implementing std::type_info functions' behavior. I'm thinking
Weird behavior when mixing loading of assemblies using Assembly.LoadFrom and Assembly.Load : I have
Should entities have behavior? or not? Why or why not? If not, does that
Can you have smart behavior for the home key in Emacs? By smart I
I have encountered odd behavior when using document.getElementById tonight. Duplicated in Firefox 3 and
I have developed some classes with similar behavior, they all implement the same interface.
I have not programmed C++ for a while and encountered a strange behavior when
I have the following code: struct B { //B() {} int x; int y;
I just wrote some code to test the behavior of std::equal, and came away
On MSVC 2005, I have the following code. std::ostringstream stream(initial string ); stream <<

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.