473,770 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting into map while using iterator?

Hello,

I would like to know if it's legal to insert one or more items into a
std::map while traversing it from begin() to end().

E.g.

for (it = mymap.begin(); it != mymap.end(); it++)
{
if (condition)
mymap[key] = data;
}

Will the iterator get invalidated?

Thanks.

-- Henrik
Oct 21 '07 #1
1 1730
On 2007-10-21 15:23, Henrik Goldman wrote:
Hello,

I would like to know if it's legal to insert one or more items into a
std::map while traversing it from begin() to end().

E.g.

for (it = mymap.begin(); it != mymap.end(); it++)
{
if (condition)
mymap[key] = data;
}

Will the iterator get invalidated?

No, inserting elements into a (multi)map/set does not invalidate
iterators, also erasing elements only invalidates iterators to the
deleted elements.

--
Erik Wikström
Oct 21 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6825
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
0
1725
by: Michael Spencer | last post by:
Wow - Alex Martelli's 'Black Magic' Pycon notes http://www.python.org/pycon/2005/papers/36/pyc05_bla_dp.pdf include this gem: > Functions 'r descriptors > def adder(x, y): return x + y > add23 = adder.__get__(23) > add42 = adder.__get__(42) > print add23(100), add42(1000) > 123 1042
5
5196
by: JC | last post by:
Hello all, I was wondering if someone can give me some tips on this problem. I'm trying to take content of a queue (Xqueue) and copy it into another queue (Yqueue) I tried using a while and/or for loop. for example if queue X contains numbers (3,5,8,6) I want to copy number (3,5) into queue Y, then I want to delete number 8
4
3138
by: PengYu.UT | last post by:
I have the following program. The auto_ptr cause the compile error. When I change auto_ptr to pointer, the error gone. Would you please help me to understand what is wrong? /*******************************************/ class iterator { private: std::auto_ptr<int> _iterator; // int *_iterator;
12
3523
by: Marcus Kwok | last post by:
I am not sure if this is something that is covered by the Standard, or if it's an implementation detail of my Standard Library. I am reading in a large amount of data into a std::set. There is an overload for std::set::insert() that takes in an iterator as a hint as to where the new value should be inserted, and my implementation (Dinkumware) says that if the hint is good (meaning the iterator points immediately before or after where...
12
4915
by: desktop | last post by:
Why does insert only work when specifying an iterator plus the object to be inserted: std::vector<intt; std::vector<int>::iterator it; it = t.begin(); t.insert(it,33); If I use push_back instead I don't need to supply the iterator. But what
10
6076
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to remove the elements with odd values from your list * and the even values from your vector.
1
2207
by: wuych | last post by:
I have a question about using iterator in template function //*****code starts here***************************** #include <vector> using std::vector; template<typename Tvoid foo( vector<T& a ) { vector<T>::iterator i; // ERROR, can't use iterator }
3
1485
by: tonyaim83 | last post by:
Hi My current problem stmt is :- There are n no. of cities each having unique name and each city having m no. of streets. I have all the information as in which street belong to which city and so on. I'm able to extract information from the given input file using the function GetName now i want to store this information in some data structure. My current implementation goes like .... string CityName; string StreetName;...
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.