473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Stuck in inf loop in a multimap iterator?

Hi,
While iterating through a multimap, I need to replace elements that meet
certain conditions with a new element.

There doesm't seem to be a replace() function for multimaps, so I'm
inserting a new element, and then deleting the old.

typedef multimap<double, MyStructType, greater<double> >;MyMultimap

MyMultimap map = ...

MyMultimap::iterator iter;

for(iter = map.begin(); iter!= map.end())
{
if(...)
{
map.insert(MyMultimap::value_type(newKey, newData));
map.erase(iter++);
}
else
++iter;
}

The for loop never exits.
I'm guessing that the iterator is messed up after the insert, but can't
see how to correct it.

Is there a way around this, or even a more efficient way to replace
elements altogether?

Thanks

Steve
Mar 14 '06 #1
8 6371

Steve Edwards wrote:
Hi,
While iterating through a multimap, I need to replace elements that meet
certain conditions with a new element.

There doesm't seem to be a replace() function for multimaps, so I'm
inserting a new element, and then deleting the old.

typedef multimap<double, MyStructType, greater<double> >;MyMultimap

MyMultimap map = ...

MyMultimap::iterator iter;

for(iter = map.begin(); iter!= map.end())
{
if(...)
{
map.insert(MyMultimap::value_type(newKey, newData));
map.erase(iter++);
}
else
++iter;
}

The for loop never exits.
I'm guessing that the iterator is messed up after the insert, but can't
see how to correct it.
Only the iterator to the deleted elemented becomes invalid. Other
iterators are not affected.

It looks like when you insert a new element it gets inserted after
iter. On a next iteration if(your new element) yields true and you
insert another element after it. That's how you probably get your
endless loop.
Is there a way around this, or even a more efficient way to replace
elements altogether?


Build a new map rather modifying an existing one and then swap the
maps. Or try boost::multi_index.

Mar 14 '06 #2
Erase returns the next iterator. So, iter = map.erase(iter) should
work.

Mar 14 '06 #3

Balaji wrote:
Erase returns the next iterator. So, iter = map.erase(iter) should
work.


There is no difference between:

iter = map.erase(iter);

and

map.erase(iter++);

Mar 14 '06 #4
map.erase(iter++)

Here, we erase what iter points to. This could invalidate iter. We then
try to increment a invalid iter.

iter = map.erase(iter)

Here, erase returns us the right next iterator.

Maxim, am I missing something?

Mar 14 '06 #5

Balaji wrote:
map.erase(iter++)

Here, we erase what iter points to. This could invalidate iter. We then
try to increment a invalid iter.
Wrong. Note the postfix increment, we increment first, then pass an old
copy to erase().
iter = map.erase(iter)

Here, erase returns us the right next iterator.

Maxim, am I missing something?


Also note that the standard (multi)map::erase does not return an
iterator. This is a non-standard extension.

Mar 14 '06 #6
Balaji wrote:
map.erase(iter++)

Here, we erase what iter points to. This could invalidate iter.
This could invalidate the pointer passed to 'map.erase()' which is,
however, a different one than 'iter'! 'iter++' moves to the next
position and then returns the original iterator. This returned
iterator is passed to 'erase()' and invalidated.
We then try to increment a invalid iter.
Nope, we don't.
iter = map.erase(iter)

Here, erase returns us the right next iterator.

Maxim, am I missing something?


Apparently you missed the use of 'operator++(int)' on the argument
to 'erase()' - or you missed how post increment works for iterators.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 14 '06 #7
In article <11*********************@i40g2000cwc.googlegroups. com>,
"Maxim Yegorushkin" <ma***************@gmail.com> wrote:
Balaji wrote:
Erase returns the next iterator. So, iter = map.erase(iter) should
work.


There is no difference between:

iter = map.erase(iter);

and

map.erase(iter++);


Thanks, rebuilding the map worked best.

Steve
Mar 14 '06 #8
a
You could use one of the several variants of the std::replace algorithm.

A
Mar 15 '06 #9

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

Similar topics

12
by: Tanguy Fautré | last post by:
Hello, does std::multimap make any guarantee about the insertion order? for example: int main() { std::multimap<int, int> Map;
9
by: Dennis Jones | last post by:
Hi, Is there a way to iterate through a multimap in such a way as to encounter only the unique keys? In other words, since a multimap allows duplicate keys, I would like to iterate through the...
3
by: Przemek | last post by:
All, what is the efficient way to find all distinct keys in a multimap. I use the code template<class T1, class T2> set<T1> GetDistinctKeys(multimap<T1, T2> nMap) { T1 temp; multimap<T1,...
4
by: Tony Young | last post by:
Hi, If I have a std::multimap<int, int> and two iterator it1 and it2 (obtained from multimap's equal_range()), how to loop the multimap from it1 to it2? I have the following loop but don't know...
3
by: giuseppe.cannella | last post by:
i must create a muiltimap with 2 string , 1 int and 1 iterator to multimap itself how could define the typedef? thank
4
by: sks | last post by:
I have a question regarding std::multimap/iterators. At the SGI website, it says "Erasing an element from a multimap also does not invalidate any iterators, except, of course, for iterators that...
1
by: Saile | last post by:
I want to give an array the values from the specific multimap's key's values. multimap<string,int> mymultimap; multimap<string,int>::iterator it;...
1
by: ambarish.mitra | last post by:
Hi all, I have a multimap, where key is an int and the value is a class. I can insert into the multimap, but finding it difficult to retrieve the value when keys match. I can do this with...
7
by: Moschops | last post by:
In moving some code from VS6 to VS2008 (bear with me, this is not a VS question, I'm just setting context), we find new crashes that weren't there before and we think they're related to trying an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.