vector insert from a set

Discussion in 'C++' started by Steven C, Nov 7, 2003.

  1. Steven C

    Steven C Guest

    vector <int> vecThe;
    set <int> setThe;

    vecThe.insert (setThe.begin (), setThe.end ());

    This gives an error about iterators not of the right type. Is there a
    way around this or do I have to set up a for loop and do
    vecThe.push_back ()?

    However I can do this:

    setThe.insert (vecThe.begin (), vecThe.end ());
     
    Steven C, Nov 7, 2003
    #1
    1. Advertising

  2. "Steven C" <> wrote in message
    news:...
    > vector <int> vecThe;
    > set <int> setThe;
    >
    > vecThe.insert (setThe.begin (), setThe.end ());
    >
    > This gives an error about iterators not of the right type. Is there a
    > way around this or do I have to set up a for loop and do
    > vecThe.push_back ()?


    The vector::insert member function overload that you want
    to call actually takes 3 parameters -- the first one being
    the position in the vector where the insertion needs to occur:

    vecThe.insert(vecThe.end(), setThe.begin(), setThe.end () );

    The alternative is to use the 'assign' function, if you want
    to discard any previous contents of the vector:
    vecThe.assign( setThe.begin (), setThe.end ());

    > However I can do this:
    >
    > setThe.insert (vecThe.begin (), vecThe.end ());


    Yes, set is different since the insertion location
    cannot be specified (value ordering is automatically
    enforced).


    Cheers,
    Ivan
    --
    http://ivan.vecerina.com
     
    Ivan Vecerina, Nov 7, 2003
    #2
    1. Advertising

  3. Steven C escribió:

    > vector <int> vecThe;
    > set <int> setThe;
    >
    > vecThe.insert (setThe.begin (), setThe.end ());


    You must specify where to insert. For example:

    vecThe.insert (vecThe.begin (), setThe.begin (), setThe.end ());

    Regards.
     
    =?iso-8859-1?Q?Juli=E1n?= Albo, Nov 7, 2003
    #3
    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:
    23,810
  2. cannontrodder
    Replies:
    1
    Views:
    734
    cannontrodder
    Jul 25, 2006
  3. Replies:
    8
    Views:
    1,927
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    565
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    363
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page