help understanding iterators with vector::insert

Discussion in 'C++' started by pwalker, Jul 31, 2007.

  1. pwalker

    pwalker Guest

    Hi everyone,

    I am trying to get my head around iterators used on vectors. Let's take the
    code below:

    -------------
    std::vector<int> v1;
    std::vector<int> v2;

    v1.push_back( 1 );
    v1.push_back( 2 );
    v1.push_back( 3 );

    v2.push_back( 10 );
    v2.push_back( 20 );
    v2.push_back( 30 );

    v1.insert(v1.end(), v2.begin(), v2.begin());

    for(int j = 0; j < v1.size(); j++)
    std::cout << v1[j] << " ";
    -------------

    I'm confused why we dont get the output 1 2 3 10

    Doesn't the insert method insert to the back of v1 everything between
    v2.begin() and v2.begin(), which is namely 10?

    I realise that to get the desired result I need to instead use
    v1.insert(v1.end(), v2.begin(), v2.begin()+1) but i fail to understand the
    logic in this.

    I know that v2.begin() points to 10, and v2.end() (which is the same as
    v2.begin()+3) points to one-beyond-30.

    I guess all it comes down to is if I specify v1.insert(v1.end(), v2.begin(),
    v2.begin()+N) it will append to the back of v1 (namely as position
    one-beyond-3) the first N values of v2.

    Any help on understanding this whole concept would be great!

    Cheers,
    Peter
    pwalker, Jul 31, 2007
    #1
    1. Advertising

  2. On Jul 31, 8:23 am, "pwalker" <> wrote:
    > Hi everyone,
    >
    > I am trying to get my head around iterators used on vectors. Let's take the
    > code below:
    >
    > -------------
    > std::vector<int> v1;
    > std::vector<int> v2;
    >
    > v1.push_back( 1 );
    > v1.push_back( 2 );
    > v1.push_back( 3 );
    >
    > v2.push_back( 10 );
    > v2.push_back( 20 );
    > v2.push_back( 30 );
    >
    > v1.insert(v1.end(), v2.begin(), v2.begin());
    >
    > for(int j = 0; j < v1.size(); j++)
    > std::cout << v1[j] << " ";
    > -------------
    >
    > I'm confused why we dont get the output 1 2 3 10
    >
    > Doesn't the insert method insert to the back of v1 everything between
    > v2.begin() and v2.begin(), which is namely 10?
    >
    > I realise that to get the desired result I need to instead use
    > v1.insert(v1.end(), v2.begin(), v2.begin()+1) but i fail to understand the
    > logic in this.


    In very simple terms, the version of vector::insert that you have used
    takes 3 parameters:
    param1: The position in vector where the insertion should take place
    param2: The position of the first element in the range of elements to
    be inserted
    param3: The position of first element beyond the range of the elements
    to be inserted.

    In other words, for the insertion, the half-open interval is used :
    [param2, param3)

    -N
    Neelesh Bodas, Jul 31, 2007
    #2
    1. Advertising

  3. pwalker

    Guest

    On Jul 31, 8:44 am, Neelesh Bodas <> wrote:
    > On Jul 31, 8:23 am, "pwalker" <> wrote:
    >
    >
    >
    > > Hi everyone,

    >
    > > I am trying to get my head around iterators used on vectors. Let's take the
    > > code below:

    >
    > > -------------
    > > std::vector<int> v1;
    > > std::vector<int> v2;

    >
    > > v1.push_back( 1 );
    > > v1.push_back( 2 );
    > > v1.push_back( 3 );

    >
    > > v2.push_back( 10 );
    > > v2.push_back( 20 );
    > > v2.push_back( 30 );

    >
    > > v1.insert(v1.end(), v2.begin(), v2.begin());

    >
    > > for(int j = 0; j < v1.size(); j++)
    > > std::cout << v1[j] << " ";
    > > -------------

    >
    > > I'm confused why we dont get the output 1 2 3 10

    >
    > > Doesn't the insert method insert to the back of v1 everything between
    > > v2.begin() and v2.begin(), which is namely 10?

    >
    > > I realise that to get the desired result I need to instead use
    > > v1.insert(v1.end(), v2.begin(), v2.begin()+1) but i fail to understand the
    > > logic in this.

    >
    > In very simple terms, the version of vector::insert that you have used
    > takes 3 parameters:
    > param1: The position in vector where the insertion should take place
    > param2: The position of the first element in the range of elements to
    > be inserted
    > param3: The position of first element beyond the range of the elements
    > to be inserted.
    >
    > In other words, for the insertion, the half-open interval is used :
    > [param2, param3)
    >
    > -N


    Hi,

    you need to have this
    v1.insert(v1.end(), v2.begin(), v2.begin()+1);

    instead of
    v1.insert(v1.end(), v2.begin(), v2.begin());

    to get the output 1 2 3 10

    Thanks
    , Jul 31, 2007
    #3
  4. pwalker

    Jim Langston Guest

    "pwalker" <> wrote in message
    news:46aeaaf1$...
    > Hi everyone,
    >
    > I am trying to get my head around iterators used on vectors. Let's take
    > the code below:
    >
    > -------------
    > std::vector<int> v1;
    > std::vector<int> v2;
    >
    > v1.push_back( 1 );
    > v1.push_back( 2 );
    > v1.push_back( 3 );
    >
    > v2.push_back( 10 );
    > v2.push_back( 20 );
    > v2.push_back( 30 );
    >
    > v1.insert(v1.end(), v2.begin(), v2.begin());
    >
    > for(int j = 0; j < v1.size(); j++)
    > std::cout << v1[j] << " ";
    > -------------
    >
    > I'm confused why we dont get the output 1 2 3 10
    >
    > Doesn't the insert method insert to the back of v1 everything between
    > v2.begin() and v2.begin(), which is namely 10?
    >
    > I realise that to get the desired result I need to instead use
    > v1.insert(v1.end(), v2.begin(), v2.begin()+1) but i fail to understand the
    > logic in this.
    >
    > I know that v2.begin() points to 10, and v2.end() (which is the same as
    > v2.begin()+3) points to one-beyond-30.
    >
    > I guess all it comes down to is if I specify v1.insert(v1.end(),
    > v2.begin(), v2.begin()+N) it will append to the back of v1 (namely as
    > position one-beyond-3) the first N values of v2.
    >
    > Any help on understanding this whole concept would be great!


    Because the third paramater is one past the one to be inserted, the reason
    being so you can use .end() which is one past the end of a container. Thsi
    way we can code:

    v1.insert( v1.end(), v2.begin(), v2.end() );
    which is more common and not have to do
    v1.insert( v1.end(), v2.begin(), v2.end() - 1 );
    Jim Langston, Jul 31, 2007
    #4
  5. pwalker

    Bo Persson Guest

    Jim Langston wrote:
    :: "pwalker" <> wrote in message
    :: news:46aeaaf1$...
    ::: Hi everyone,
    :::
    ::: I am trying to get my head around iterators used on vectors.
    ::: Let's take the code below:
    :::
    ::: -------------
    ::: std::vector<int> v1;
    ::: std::vector<int> v2;
    :::
    ::: v1.push_back( 1 );
    ::: v1.push_back( 2 );
    ::: v1.push_back( 3 );
    :::
    ::: v2.push_back( 10 );
    ::: v2.push_back( 20 );
    ::: v2.push_back( 30 );
    :::
    ::: v1.insert(v1.end(), v2.begin(), v2.begin());
    :::
    ::: for(int j = 0; j < v1.size(); j++)
    ::: std::cout << v1[j] << " ";
    ::: -------------
    :::
    ::: I'm confused why we dont get the output 1 2 3 10
    :::
    ::: Doesn't the insert method insert to the back of v1 everything
    ::: between v2.begin() and v2.begin(), which is namely 10?
    :::
    ::: I realise that to get the desired result I need to instead use
    ::: v1.insert(v1.end(), v2.begin(), v2.begin()+1) but i fail to
    ::: understand the logic in this.
    :::
    ::: I know that v2.begin() points to 10, and v2.end() (which is the
    ::: same as v2.begin()+3) points to one-beyond-30.
    :::
    ::: I guess all it comes down to is if I specify v1.insert(v1.end(),
    ::: v2.begin(), v2.begin()+N) it will append to the back of v1
    ::: (namely as position one-beyond-3) the first N values of v2.
    :::
    ::: Any help on understanding this whole concept would be great!
    ::
    :: Because the third paramater is one past the one to be inserted,
    :: the reason being so you can use .end() which is one past the end
    :: of a container. Thsi way we can code:
    ::
    :: v1.insert( v1.end(), v2.begin(), v2.end() );
    :: which is more common and not have to do
    :: v1.insert( v1.end(), v2.begin(), v2.end() - 1 );

    And also, the first version works even if v2 is empty. The last one
    does definitely not!


    Bo Persson
    Bo Persson, Jul 31, 2007
    #5
  6. pwalker wrote:
    > Doesn't the insert method insert to the back of v1 everything between
    > v2.begin() and v2.begin(), which is namely 10?


    There's nothing *between* v2.begin() and v2.begin(). It's a
    zero-sized range, and thus contains nothing.
    Juha Nieminen, Jul 31, 2007
    #6
    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. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    484
    Kai-Uwe Bux
    May 8, 2005
  2. David Crawford

    Question on vector of vector iterators

    David Crawford, Dec 15, 2005, in forum: C++
    Replies:
    3
    Views:
    477
  3. Replies:
    8
    Views:
    1,913
    Csaba
    Feb 18, 2006
  4. Alex Vinokur
    Replies:
    5
    Views:
    380
    Ron Natalie
    Jul 12, 2006
  5. , India
    Replies:
    10
    Views:
    1,072
    James Kanze
    Aug 8, 2009
Loading...

Share This Page