vector insert from a set

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven C

    vector insert from a set

    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_bac k ()?

    However I can do this:

    setThe.insert (vecThe.begin (), vecThe.end ());
  • Ivan Vecerina

    #2
    Re: vector insert from a set

    "Steven C" <steven1832@hot mail.com> wrote in message
    news:767f8298.0 311071354.527d3 758@posting.goo gle.com...[color=blue]
    > 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_bac k ()?[/color]

    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(v ecThe.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 ());
    [color=blue]
    > However I can do this:
    >
    > setThe.insert (vecThe.begin (), vecThe.end ());[/color]

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


    Cheers,
    Ivan
    --
    Ivan Vecerina - expert in medical devices, software - info, links, contact information, code snippets





    Comment

    • Julián Albo

      #3
      Re: vector insert from a set

      Steven C escribió:
      [color=blue]
      > vector <int> vecThe;
      > set <int> setThe;
      >
      > vecThe.insert (setThe.begin (), setThe.end ());[/color]

      You must specify where to insert. For example:

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

      Regards.

      Comment

      Working...