Copy vector to set

Discussion in 'C++' started by Xenos, Sep 21, 2004.

  1. Xenos

    Xenos Guest

    I have elements of vector I need to add to a set. Can I just do (ignoring
    namespace):

    copy(v.begin(), v.end(), inserter(s, s.end()));

    Where, v is a vector and s is a set, and both have the same value_type.

    I'm worried about the iterator required by inserter, and thus insert. I
    know a map will just use the iterator as a hint, and still place the items
    in the proper order. Will set also do this, or will my above call mess up
    the ordering?


    Thanks,

    DrX
     
    Xenos, Sep 21, 2004
    #1
    1. Advertising

  2. Xenos wrote:
    > I have elements of vector I need to add to a set. Can I just do (ignoring
    > namespace):
    >
    > copy(v.begin(), v.end(), inserter(s, s.end()));
    >
    > Where, v is a vector and s is a set, and both have the same value_type.
    >
    > I'm worried about the iterator required by inserter, and thus insert. I
    > know a map will just use the iterator as a hint, and still place the items
    > in the proper order. Will set also do this, or will my above call mess up
    > the ordering?


    No mess-up should happen. std::insert_iterator just causes the set to
    insert. So, whatever std::set::insert does, you'll get.

    V
     
    Victor Bazarov, Sep 21, 2004
    #2
    1. Advertising

  3. Xenos

    Mark Wright Guest

    One joyful day (Tue, 21 Sep 2004 16:46:00 -0400 to be precise), "Xenos"
    <> decided that the Usenet community would
    benefit from this remarkable comment:

    >I have elements of vector I need to add to a set. Can I just do (ignoring
    >namespace):
    >
    >copy(v.begin(), v.end(), inserter(s, s.end()));


    Yes.

    >Where, v is a vector and s is a set, and both have the same value_type.
    >
    >I'm worried about the iterator required by inserter, and thus insert. I
    >know a map will just use the iterator as a hint, and still place the items
    >in the proper order. Will set also do this, or will my above call mess up
    >the ordering?


    Sets are ordered by definition, so it doesn't matter how or where you
    insert. Also, neither map nor set will use the iterator as a hint, to do
    so would corrupt the ordering, unless the iterator happened to be at the
    correct place for the object to be inserted.

    Mark Wright
    -

    ================Today's Thought====================
    "In places where books are burned, one day,
    people will be burned" - Heinrich Heine, Germany -
    100 years later, Hitler proved him right
    ===================================================
     
    Mark Wright, Sep 22, 2004
    #3
  4. Xenos

    Xenos Guest

    "Mark Wright" <> wrote in message
    news:...
    > One joyful day (Tue, 21 Sep 2004 16:46:00 -0400 to be precise), "Xenos"
    > <> decided that the Usenet community would
    > benefit from this remarkable comment:
    >
    > >I have elements of vector I need to add to a set. Can I just do

    (ignoring
    > >namespace):
    > >
    > >copy(v.begin(), v.end(), inserter(s, s.end()));

    >
    > Yes.
    >
    > >Where, v is a vector and s is a set, and both have the same value_type.
    > >
    > >I'm worried about the iterator required by inserter, and thus insert. I
    > >know a map will just use the iterator as a hint, and still place the

    items
    > >in the proper order. Will set also do this, or will my above call mess

    up
    > >the ordering?

    >
    > Sets are ordered by definition, so it doesn't matter how or where you
    > insert. Also, neither map nor set will use the iterator as a hint, to do
    > so would corrupt the ordering, unless the iterator happened to be at the
    > correct place for the object to be inserted.


    Stroustrup's Book (the 3rd edition) says the iterator is used as a hint of
    where to start searching for the correct point of insertion, it doesn't
    corrupt the ordering. Whether it really does use it as a hint or not by the
    standard, I don't really care. I just care that set will also not corrupt
    the ordering, which you and Mr. B. says it won't. So, Thanks!

    DrX
     
    Xenos, Sep 22, 2004
    #4
  5. Xenos

    Mark Wright Guest

    One joyful day (Wed, 22 Sep 2004 13:15:06 -0400 to be precise), "Xenos"
    <> decided that the Usenet community would
    benefit from this remarkable comment:

    >Stroustrup's Book (the 3rd edition) says the iterator is used as a hint of
    >where to start searching for the correct point of insertion, it doesn't
    >corrupt the ordering.


    Ah, sorry, I didn't know that (even though I've had that book for
    several years!).

    Mark Wright
    -

    ================Today's Thought====================
    "In places where books are burned, one day,
    people will be burned" - Heinrich Heine, Germany -
    100 years later, Hitler proved him right
    ===================================================
     
    Mark Wright, Sep 23, 2004
    #5
    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. pmatos
    Replies:
    6
    Views:
    24,125
  2. Alex
    Replies:
    2
    Views:
    1,288
  3. Replies:
    8
    Views:
    1,999
    Csaba
    Feb 18, 2006
  4. Replies:
    26
    Views:
    2,176
    Roland Pibinger
    Sep 1, 2006
  5. Javier
    Replies:
    2
    Views:
    622
    James Kanze
    Sep 4, 2007
Loading...

Share This Page