Sequence algorithm usage help

R

[rob desbois]

I've not really used sequence algorithms before and am having some
trouble interpreting usage from The C++ Programming Language
[Stroustrup].

The effect I'd like to achieve is to copy all entries (key and value)
from map<string, string> source to dest - without overwriting any
entries in dest unless the keys match, ie:

for (map<string, string>::const_iterator p = source.begin(); p !=
source.end(); ++p)
dest[p->first] = dest[p->second];

AFAICT copy would overwrite the first N elements of dest with those of
source, where N = source.size(). Is this correct? Is there an
appropriate sequence algorithm that will achieve the code shown above?

--Rob
 
E

eriwik

I've not really used sequence algorithms before and am having some
trouble interpreting usage from The C++ Programming Language
[Stroustrup].

The effect I'd like to achieve is to copy all entries (key and value)
from map<string, string> source to dest - without overwriting any
entries in dest unless the keys match, ie:

So you want to insert all elements in source into dest and overwrite
any elements in dest having the same key as in source? Try the
following:

dest.insert(source.begin(), source.end());
 
E

eriwik

I've not really used sequence algorithms before and am having some
trouble interpreting usage from The C++ Programming Language
[Stroustrup].

The effect I'd like to achieve is to copy all entries (key and value)
from map<string, string> source to dest - without overwriting any
entries in dest unless the keys match, ie:

for (map<string, string>::const_iterator p = source.begin(); p !=
source.end(); ++p)
dest[p->first] = dest[p->second];

AFAICT copy would overwrite the first N elements of dest with those of
source, where N = source.size(). Is this correct? Is there an
appropriate sequence algorithm that will achieve the code shown above?

The code above have a quite peculiar function, it finds the element in
dest that have the key p->second and assigns the value of this element
to the element in dest that has the key p->fist.

You might have meant to do dest[p->first] = p->second, which would
create (or ovrewrite) a new element in dest with key p->fist and value
p->second, which is the same as copying the element p points to into
dest. Another way would be to use dest.insert(*p), if you just want to
insert a single element. To copy all elements see my other post.
 
R

[rob desbois]

map's insert(iter, iter) operation won't overwrite entries where the
key already exists, it will only add new entries where the key does not
exist.

--Rob
 
R

[rob desbois]

for (map<string, string>::const_iterator p = source.begin(); p !=
source.end(); ++p)
dest[p->first] = dest[p->second];

The code above have a quite peculiar function, it finds the element in
dest that have the key p->second and assigns the value of this element
to the element in dest that has the key p->fist.

You might have meant to do dest[p->first] = p->second, which would
create (or ovrewrite) a new element in dest with key p->fist and value
p->second, which is the same as copying the element p points to into
dest. Another way would be to use dest.insert(*p), if you just want to
insert a single element. To copy all elements see my other post.

Whoops - yes, dest[p->first] = p->second was what I meant to write.
Regarding copying all elements *with overwrite*, see my previous post.
 
E

eriwik

map's insert(iter, iter) operation won't overwrite entries where the
key already exists, it will only add new entries where the key does not
exist.

Seems like you are right, we learn new things every day. You can
however use something like this:

for (std::map<int, int>::iterator p = src.begin(); p != src.end(); ++p)
dst.insert(*p);
 
R

[rob desbois]

for (std::map<int, int>::iterator p = src.begin(); p != src.end(); ++p)
dst.insert(*p);

This is almost equivalent to my code (although better - hence I would
use it in preference).
The reason I have posted is to see if there is a STL algorithm for this
purpose so I can remove the need for an explicit loop.

--Rob
 
R

[rob desbois]

I have discovered someone else's solution to this:
source.insert(dest.begin(), dest.end());
dest.swap(source);

It copies *without overwrite* to the intended source map, then simply
swaps the contents of both maps. Obviously if the source map was not
intended to be modified a copy will need to be made somewhere along the
line, but otherwise it's an effective mechanism, especially as it still
only involves a single implicit loop in the copy, and the simple fast
swap() operation.

Thanks for your input Erik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top