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 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

Consider the following: std::vector<int> vec(1); // vector has one element std::fill(vec.begin(), vec.begin(), 42); std::fill(vec.begin()+1,
Lets say I have a std::vector of const std::string s. std::vector<const std::string> strs; Now
I wanted to know the behavior of std::vector::reserve() in following situations: Suppose reserve(N) is
I have some question about behavior of std::async function with std::launch::async policy & std::future
If I have a std::vector buffer that has room for 100 characters and I
I know that compilers have much freedom in implementing std::type_info functions' behavior. I'm thinking
What a title, suppose I have a map like this: std::map<int, int> m; and
Say I have the following: std::unique_ptr<A> pA; pA(new A); In this convoluted example, what
How do I explain the following behavior? #include<iostream> using namespace std; int main(){ unsigned
Take the following code: std::vector<std::vector<int>> v(10, 10); This code doesn't compile with libstdc++. It

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.