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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:20:15+00:00 2026-06-06T06:20:15+00:00

While I use iterator like this , //include header files using namespace std; int

  • 0

While I use iterator like this ,

//include header files
using namespace std;

int main()
{
    map<int,int> intIntMap;
    map<int,int>::iterator pos;
    pos = intIntMap.begin();

    intIntMap[0] = 1;
    intIntMap[3] = 5;
    intIntMap[4] = 9;
    intIntMap[5] = 5;

    //遍历
    cout << (*pos).first << endl;

    while( pos != intIntMap.end() )
    {
        cout << pos->first << " <---> " << pos->second << endl;
        pos++;
    }

}

The output is 4;

But while I use iterator like this:

//include header file
using namespace std;

int main()
{
    map<int,int> intIntMap;
    map<int,int>::iterator pos;

    intIntMap[0] = 1;
    intIntMap[3] = 5;
    intIntMap[4] = 9;
    intIntMap[5] = 5;

    //遍历
    pos = intIntMap.begin();
    cout << (*pos).first << endl;

    while( pos != intIntMap.end() )
    {
        cout << pos->first << " <---> " << pos->second << endl;
        pos++;
    }

}

The output is what I want;

I want to know what is the difference between use of iterator, what happended to the first iterator when I insert new key-value pair ? Thanks!

addtion:
compile is use gcc 4.1.2 , in feel more confused,like this :

compile is use gcc 4.1.2 , in feel more confused,like this :

  • 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-06T06:20:17+00:00Added an answer on June 6, 2026 at 6:20 am

    Since you called begin() when the container was empty, you got an iterator that was equal to end() (§23.1/7: “If the container is empty, then begin() == end()”).

    Inserting items into the container didn’t change that, so you still have pos == intIntMap.end().

    You then execute zero iterations of your loop, since pos==end(), and you’r executing the loop only as long as pos != end().

    In the second example, you set pos() after you’ve inserted the data, so you get the first items in the collection, and iterate to the last.

    Edit: As far as printing out the contents of the map goes, I’d probably do it more like this:

    std::ostream &operator<<(std::ostream &os, std::pair<int, int> const &d) { 
        return os << d.first << " <---> " << d.second;
    }
    
    // ...
    
    std::copy(intIntMap.begin(), intIntMap.end(), 
              std::ostream_iterator<std::pair<int, int> >(std::cout, "\n"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use something like this: map<string, Data>::iterator it = mymap->begin(); map<string, Data>::iterator end =
I am getting some problems while trying to use this regex: (public +function|private +function|function)
I'm receiving this PHP error while trying to use the Normalizer class, specifically the
What I would like to do is use the elegance of LINQ while maintaining
I have a 5yr old G4 PowerBook that I use while travelling, and I
I have a question, which Unicode encoding to use while encoding .NET string into
I have a CSV file with set of records on it. While I use
While trying to use LINQ in a .NET 2.0 Windows Forms project I got:
While trying to use XMLWorkerHelper.GetInstance().ParseXHTML() i find that it is really strict. Any wrong
Getting system.entrypointnotfoundexception: loadlibrary While trying to use SevenZipLib.dll to uncompress the .7z file containing

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.