Sequence algorithm usage help

Discussion in 'C++' started by [rob desbois], Dec 5, 2006.

  1. 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
     
    [rob desbois], Dec 5, 2006
    #1
    1. Advertising

  2. [rob desbois]

    Guest

    On Dec 5, 12:23 pm, "[rob desbois]" <> wrote:
    > 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());

    --
    Erik Wikström
     
    , Dec 5, 2006
    #2
    1. Advertising

  3. [rob desbois]

    Guest

    On Dec 5, 12:23 pm, "[rob desbois]" <> wrote:
    > 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.

    --
    Erik Wikström
     
    , Dec 5, 2006
    #3
  4. 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
     
    [rob desbois], Dec 5, 2006
    #4
  5. wrote:
    > > 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.
     
    [rob desbois], Dec 5, 2006
    #5
  6. [rob desbois]

    Guest

    On Dec 5, 1:02 pm, "[rob desbois]" <> wrote:
    > 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);

    --
    Erik Wikström
     
    , Dec 5, 2006
    #6
  7. wrote:
    > 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
     
    [rob desbois], Dec 5, 2006
    #7
  8. [rob desbois]

    Guest

    On Dec 5, 1:31 pm, "[rob desbois]" <> wrote:
    > wrote:
    > > 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.


    You can find what I believe to be a complete list of the STL algorithms
    here: http://www.cppreference.com/cppalgorithm/index.html, the only one
    I saw that might work is copy, but you've already knew of that one.

    --
    Erik Wikström
     
    , Dec 5, 2006
    #8
  9. 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
     
    [rob desbois], Dec 5, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. metfan
    Replies:
    2
    Views:
    4,885
    Robert Olofsson
    Oct 21, 2003
  2. Ahmed Moustafa
    Replies:
    0
    Views:
    815
    Ahmed Moustafa
    Nov 15, 2003
  3. stef mientki
    Replies:
    13
    Views:
    690
    stef mientki
    Oct 20, 2007
  4. jimxoch
    Replies:
    4
    Views:
    320
    jimxoch
    Jul 24, 2008
  5. Peter Brooks
    Replies:
    17
    Views:
    162
    Robert Kern
    Jun 12, 2013
Loading...

Share This Page