Re: Help with some logic, best way to update a container.
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Sun, 08 Feb 2009 19:25:28 -0500
See below...
On Sun, 08 Feb 2009 14:29:31 -0600, "Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote:
On Sun, 08 Feb 2009 02:41:57 -0500, Joseph M. Newcomer****
<newcomer@xxxxxxxxxxxx> wrote:
for(std::map<int, int>::const_iterator iter = newValues.begin(); iter != newValues.end();
++iter)
{ /* add values in */
OldValues.insert(std::pair(iter->first, iter->second));
Could be simply OldValues.insert(*iter). Also, it won't compile because
pair is a template.
That's the risk of trying to type something like this in from memory, without any code or
documentation in front of me. Your'e absolutely right
****
For those times when you actually have to construct a****
pair, you could use make_pair to avoid having to specify the template
parameters, but it's better practice to declare a typedef MapT and use
MapT::value_type(x, y), as it can avoid conversions due to the type of the
key being const K and not just plain K. It also allows you to pass string
literals when (say) std::string is the key or value type.
} /* add values in */
(or something close to this; I'm still not always able to type this stuff in straight, and
I've not got my reference material available)
Note that insert to a map will either add a new value if the value does not exist, or
replace the existing value if it already exists.
Actually, map::insert will *not* update an existing item; it either inserts
or does nothing. You can tell what happened by examining its return value,
and if you need to update, you will also have an iterator to the existing
item. However, if you need an "insert_or_update" function, the easiest way
is to use operator[].
Now that I look at my code, I see that indeed I use the <iterator, bool> return from
insert to decide to replace the iterator->second member. Should have gone off to find my
code before answering. But that says it takes even more effort to cause the code to
simulate a simple assignment of OldValue = NewValue
****
****
So note that A=1 really stands for, what
you might implement in a map, as 65=1 (treating the letters as integers because you gave
your sample map as <int, int> and I don't want to change my example around too much
because it is hard to talk about 65=1)
So that takes care of everything except deleting the elements which are not in newValues.
So, I *could* do
for(std::map<int,int>::iterator iter = OldValues.begin(); iter != OldValues.end(); ++iter)
{
if(newValues.find(iter->first) == newValues.end())
{ /* delete it */
OldValues.erase(iter);
} /* delete it */
Incrementing iter is undefined after you erase it. The correct pattern is
something like:
I'd actually checked that, and it was not clear what happened.
As usual, the documentation is sloppy; the erase method does not state this behavior. I
presume we are to infer this behavior by ethereal vibrations (if it isn't mentioned in
erase, it doesn't matter if it is mentioned anywhere else). Another candidate for my
errors and omissions...
****
Joseph M. Newcomer [MVP]
for (i = m.begin(); i != m.end(); )
{
if (need to erase)
m.erase(i++); // NB: Post-increment is necessary here.
else
++i;
}
For containers whose erase returns an iterator, you must use:
for (i = m.begin(); i != m.end(); )
{
if (need to erase)
i = m.erase(i);
else
++i;
}
ISTR VC's map::erase returns an iterator, but that's non-standard.
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- Help with some logic, best way to update a container.
- From: Simon
- Re: Help with some logic, best way to update a container.
- From: Joseph M . Newcomer
- Re: Help with some logic, best way to update a container.
- From: Simon
- Re: Help with some logic, best way to update a container.
- From: Joseph M . Newcomer
- Re: Help with some logic, best way to update a container.
- From: Simon
- Re: Help with some logic, best way to update a container.
- From: Joseph M . Newcomer
- Re: Help with some logic, best way to update a container.
- From: Doug Harrison [MVP]
- Help with some logic, best way to update a container.
- Prev by Date: Re: Help with some logic, best way to update a container.
- Next by Date: Word Capture
- Previous by thread: Re: Help with some logic, best way to update a container.
- Next by thread: Re: Help with some logic, best way to update a container.
- Index(es):
Relevant Pages
|
Loading