473,385 Members | 1,720 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.

erase all pairs with a specified value in a std::map

i am looking for a more elegant/standard way to do this simple
operation,

however, for now, i just coded this beast. any comments will be
grateful,

ObserverMap::iterator itr = observers_.begin();
while(itr != observers_.end()){
if(itr->second == observer){
ObserverMap::iterator tmpItr = ++itr;
--itr;
observers_.erase(itr);
itr = tmpItr;
continue;
}

++itr;
}

- minglei
Jun 27 '08 #1
3 1148
ke****@gmail.com wrote:
i am looking for a more elegant/standard way to do this simple
operation,

however, for now, i just coded this beast. any comments will be
grateful,

ObserverMap::iterator itr = observers_.begin();
while(itr != observers_.end()){
if(itr->second == observer){
ObserverMap::iterator tmpItr = ++itr;
--itr;
observers_.erase(itr);
itr = tmpItr;
OK, maybe I'm stating the obvious here, but the above FOUR lines
can be rewritten as just one:

observers_.erase(itr++);
continue;
Drop the 'continue' and add 'else' below.
}
add
else

here.
>
++itr;
}

- minglei
IOW, you should be able to do what you need like so:

ObserverMap::iterator itr = observers_.begin();
while (itr != observers_.end()) {
if (itr->second == observer)
observers_.erase(itr++);
else
++itr;
}

How's that?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
IOW, you should be able to do what you need like so:

ObserverMap::iterator itr = observers_.begin();
while (itr != observers_.end()) {
if (itr->second == observer)
observers_.erase(itr++);
else
++itr;
}

How's that?

V
cool, that's really cool, a little arrange of code.

for(ObserverMap::iterator itr = observers_.begin(); itr !=
observers_.end(); )
if (itr->second == observer)
observers_.erase(itr++);
else
++itr;

- minglei
Jun 27 '08 #3
Hi!

Victor Bazarov schrieb:
IOW, you should be able to do what you need like so:

ObserverMap::iterator itr = observers_.begin();
while (itr != observers_.end()) {
if (itr->second == observer)
observers_.erase(itr++);
else
++itr;
}
Now we consolidate the increment ++itr:

ObserverMap::iterator itr = observers_.begin();
while (itr != observers_.end()) {
const ObserverMap::iterator current = itr++;
if (current->second == observer)
observers_.erase(current);
}

This is less confusing to me.

Regards, Frank
Jun 27 '08 #4

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

Similar topics

3
by: Woodster | last post by:
I have declared the following std::map<std::string, std::string> myMap; to pass myMap to functions should I be declaring functions as: void function(std::map<std::string, std::string>); ...
1
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
5
by: Diwa | last post by:
Does the "value" type (value as in key-value pair )of "std::map" require a default ctor even if it is not used ? If I comment out Line 1 in the code attached later, i.e remove the default ctor...
5
by: Chris Forone | last post by:
Hello group, g++ (3.4.2, mingw): float init = {1.f, 2.f, 3.f}; std::map<std::string, std::valarray<float mp; mp = std::valarray(init, 3); mp.size(); // should be 3, but IS 0!
8
by: mveygman | last post by:
Hi, I am writing code that is using std::map and having a bit of an issue with its performance. It appears that the std::map is significantly slower searching for an element then a sequential...
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
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...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.