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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:38:11+00:00 2026-06-02T20:38:11+00:00

I have the following code: int main() { vector<int> v; for(int i = 0;

  • 0

I have the following code:

int main()
{
    vector<int> v;

    for(int i = 0; i < 10; ++i)
        v.push_back(i);

    auto it = v.begin() + 3;

    cout << "Iterator: " << *it << endl;

    vector<int>::reverse_iterator revIt(it);

    cout << "Reverse iterator: " << *revIt << endl;

}

After running this code I get the following output:

Iterator: 3
Reverse iterator: 2

Could someone explain why the 2 values differ ?

  • 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-02T20:38:13+00:00Added an answer on June 2, 2026 at 8:38 pm

    Reverse iterators ‘correspond’ to a base iterator with an offset of one element because of how rbegin() and rend() have to be represented using base iterators that are valid (end() and begin() respectively). For example, rend() cannot be represented by an interator that ‘points’ before the container’s begin() iterator, although that’s what it logically represents. So rend()‘s ‘base iterator’ is begin(). Therefore, rbegin()‘s base iterator becomes end().
    A reverse iterator automatically adjusts for that offset when it is dereferenced (using the * or -> operators).

    An old article by Scott Meyers explains the relationship in detail along with a nice picture:

    Guideline 3: Understand How to Use a reverse_iterator’s Base iterator

    Invoking the base member function on a reverse_iterator yields the
    “corresponding” iterator, but it’s not really clear what that means.
    As an example, take a look at this code, which puts the numbers 1-5 in
    a vector, sets a reverse_iterator to point to the 3, and sets an
    iterator to the reverse_iterator’s base:

    vector<int> v;
    
    // put 1-5 in the vector
    for (int i = 1; i <= 5; ++i) {
      v.push_back(i);
    }
    
    // make ri point to the 3
    vector<int>::reverse_iterator ri =
      find(v.rbegin(), v.rend(), 3);
    
    // make i the same as ri's base
    vector<int>::iterator i(ri.base());
    

    After executing this code, things can be thought of as looking like
    this:

    alt text

    This picture is nice, displaying the characteristic offset of a
    reverse_iterator and its corresponding base iterator that mimics the
    offset of rbegin() and rend() with respect to begin() and end(), but
    it doesn’t tell you everything you need to know. In particular, it
    doesn’t explain how to use i to perform operations you’d like to
    perform on ri.

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

Sidebar

Related Questions

I have the following code: int main(int argc, char** argv) { onelog a; std::cout
I have the following code: #include <iostream> #include <vector> #include <algorithm> int main( int
I have the following code int main() { int a=6; void *p; p=&a; p++;
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
I have tried to use the following snippet of code: int main() { string
I have the following code #include <algorithm> #include <iostream> #include <vector> #include <functional> int
I have some trouble understanding the output from the following code: int main(void) {
I have the following code #include <iostream> #include <vector> using namespace std; int distance(vector<int>&
i have following code for copying content of vector into file #include<iterator> #include<algorithm> #include<fstream>

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.