How to perform a move between STL containers

Discussion in 'C++' started by Dennis Jones, Jan 3, 2007.

  1. Dennis Jones

    Dennis Jones Guest

    Hi,

    Is there is a way to *move* elements from one container to another with a
    single STL function? That is, can I move an element (or range of elements)
    between containers without doing a copy followed by a remove?

    I've got two containers (a queue and a list) holding shared_ptr's, and I'd
    like to move elements back and forth between them. I only need to move one
    element at a time, though I suspect a general solution would also accept a
    range.

    Thanks,

    Dennis
     
    Dennis Jones, Jan 3, 2007
    #1
    1. Advertising

  2. Dennis Jones wrote:

    > I've got two containers (a queue and a list) holding shared_ptr's, and I'd
    > like to move elements back and forth between them.


    What problem are you trying to solve? Using smart pointers copying and
    deleting causes no problem. If you want to do it with a single function,
    write that function.

    There is the splice function that does what you want, but just for lists of
    the same type. This makes sense because the list implementation can use his
    internal implementation details to do it in a way much more efficient than
    a external function can do.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 3, 2007
    #2
    1. Advertising

  3. Dennis Jones

    Dennis Jones Guest

    "Julián Albo" <> wrote in message
    news:...
    > Dennis Jones wrote:
    >
    >> I've got two containers (a queue and a list) holding shared_ptr's, and
    >> I'd
    >> like to move elements back and forth between them.

    >
    > What problem are you trying to solve? Using smart pointers copying and
    > deleting causes no problem. If you want to do it with a single function,
    > write that function.
    >
    > There is the splice function that does what you want, but just for lists
    > of
    > the same type. This makes sense because the list implementation can use
    > his
    > internal implementation details to do it in a way much more efficient than
    > a external function can do.


    No problem in particular...I'm just looking for the appropriate built-in
    solution, if one exists (instead of writing my own) -- no sense reinventing
    the wheel if it's already invented!

    - Dennis
     
    Dennis Jones, Jan 3, 2007
    #3
  4. On Wed, 03 Jan 2007 01:33:52 GMT, "Dennis Jones" wrote:
    >Is there is a way to *move* elements from one container to another with a
    >single STL function? That is, can I move an element (or range of elements)
    >between containers without doing a copy followed by a remove?
    >I've got two containers (a queue and a list) holding shared_ptr's,


    shared_ptr's in STL containers??

    >and I'd
    >like to move elements back and forth between them. I only need to move one
    >element at a time, though I suspect a general solution would also accept a
    >range.


    For list look at the splice functions, for queue you are out of luck.

    Best wishes,
    Roland Pibinger
     
    Roland Pibinger, Jan 3, 2007
    #4
  5. Roland Pibinger wrote:
    > On Wed, 03 Jan 2007 01:33:52 GMT, "Dennis Jones" wrote:
    >> Is there is a way to *move* elements from one container to another with a
    >> single STL function? That is, can I move an element (or range of elements)
    >> between containers without doing a copy followed by a remove?
    >> I've got two containers (a queue and a list) holding shared_ptr's,

    >
    > shared_ptr's in STL containers??


    I'm intrigued as to why that seems noteworthy to you.

    --
    Clark S. Cox III
     
    Clark S. Cox III, Jan 3, 2007
    #5
  6. Dennis Jones

    Dennis Jones Guest

    "Clark S. Cox III" <> wrote in message
    news:...
    > Roland Pibinger wrote:
    >> On Wed, 03 Jan 2007 01:33:52 GMT, "Dennis Jones" wrote:
    >>> Is there is a way to *move* elements from one container to another with
    >>> a
    >>> single STL function? That is, can I move an element (or range of
    >>> elements)
    >>> between containers without doing a copy followed by a remove?
    >>> I've got two containers (a queue and a list) holding shared_ptr's,

    >>
    >> shared_ptr's in STL containers??

    >
    > I'm intrigued as to why that seems noteworthy to you.


    Me too -- I'm not sure if Clark is questioning its usefulness or its
    validity.

    In either case, Clark, it's both useful and valid.

    - Dennis
     
    Dennis Jones, Jan 3, 2007
    #6
  7. Dennis Jones wrote:
    > "Clark S. Cox III" <> wrote in message
    > news:...
    >> Roland Pibinger wrote:
    >>> On Wed, 03 Jan 2007 01:33:52 GMT, "Dennis Jones" wrote:
    >>>> Is there is a way to *move* elements from one container to another with
    >>>> a
    >>>> single STL function? That is, can I move an element (or range of
    >>>> elements)
    >>>> between containers without doing a copy followed by a remove?
    >>>> I've got two containers (a queue and a list) holding shared_ptr's,
    >>> shared_ptr's in STL containers??

    >> I'm intrigued as to why that seems noteworthy to you.

    >
    > Me too -- I'm not sure if Clark is questioning its usefulness or its
    > validity.
    >
    > In either case, Clark, it's both useful and valid.


    Oh, I know that it is both useful and valid, and am questioning neither.
    I just wondered what Roland was questioning.

    --
    Clark S. Cox III
     
    Clark S. Cox III, Jan 3, 2007
    #7
  8. Dennis Jones

    Dennis Jones Guest

    "Clark S. Cox III" <> wrote in message
    news:...

    > Oh, I know that it is both useful and valid, and am questioning neither.
    > I just wondered what Roland was questioning.


    Oh, sorry! :) I was intending to refer to "Roland" (not "Clark") since he
    was the one that asked the question. Too early in the morning for me, I
    guess!

    - Dennis
     
    Dennis Jones, Jan 3, 2007
    #8
    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. Replies:
    7
    Views:
    555
    Pete Becker
    Jan 25, 2008
  2. Andrey Vul
    Replies:
    6
    Views:
    575
    James Kanze
    Oct 22, 2009
  3. Eduardo78
    Replies:
    0
    Views:
    258
    Eduardo78
    Nov 3, 2005
  4. Andrew Tomazos
    Replies:
    2
    Views:
    629
    Nobody
    Dec 12, 2011
  5. Sebastian Mach
    Replies:
    5
    Views:
    315
Loading...

Share This Page