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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:50:06+00:00 2026-05-28T18:50:06+00:00

Is it correctly possible to walk sequently through QMap with help of iterators, and

  • 0

Is it correctly possible to walk sequently through QMap with help of iterators, and doing such actions: removing some items and adding new ones?

For example:

for( QMap<key_t,val_t>::iterator it = map.begin();
     it != map.end();
     ++it )
{
     if( it->value == something )
     {
          map.erase(it);
          map.insert(it->key+10,it->value); 
     } 
}

It seems, that nothing will be done wrong, I’m asking to be sure. (I have no enough time to check it).

UPD Will solve with QMap::unite():

for( QMap<key_t,val_t>::iterator it = map.begin();
     it != map.end();
     ++it )
{
     if( it->value == something )
     {
          tmp_map.insert(it->key+10,it->value); 
          map.erase(it);
     } 
}
map.unite(tmp_map);

Thanks for answers!

  • 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-28T18:50:08+00:00Added an answer on May 28, 2026 at 6:50 pm

    Think about it a little while… You are iterating over a collection, removing an item in the middle and adding another item somewhere else. Will the iterators still be correct? Will the “next” iterator really be the next item?

    In general it is not a good idea to change a collection you are iterating over. If you need to then use a temporary collection and copy selected items over to that, and clear the real collection and move the items from the temporary collection over to the real one.

    In your case though, why not use QMap::find to search for something, and if found erase it and add the new item, and do it in a loop until something is not found anymore?

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

Sidebar

Related Questions

Possible Duplicate: How to correctly unregister an event handler MSDN states the following two
Is it currently possible to compile Python and PyObjC for the iPhone such that
Is it possible to correctly pass an OracleParameter to a boolean parameter in a
Do I suppose correctly that it is not possible to create view with parameters?
Possible Duplicate: Unicode in C++ If I remembered correctly, the default character and string
Possible Duplicate: Space Before Closing Slash? Hi, If I remember correctly we have it
Hi would it be possible to correctly split function attributes using regex ? i
Possible Duplicate: Correctly encode characters in a PHP mail form (“I'm” turns to be
Possible Duplicate: Javascript fails to access a JSF component by calling through its id
Is it possible to get Log4perl to correctly display the line number and package/class

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.