Bizarre vector insertion behavior - can someone please enlighten?

Discussion in 'C++' started by ckfan.painter@gmail.com, Mar 7, 2007.

  1. Guest

    I've run into a seemingly bizarre problem with insert() for
    std::vector. (This was done on Microsoft Visual C++ 2005 express
    version 8...maybe it is a compiler specific bug?)

    Here's the code:

    //===================

    // vector tester 3.cpp : main project file.

    #include "stdafx.h"
    #include<vector>

    int main(array<System::String ^> ^args)
    {
    std::vector<int> vectorList;

    for (unsigned int i = 0; i < 45; ++i) // works if 45 is
    replaced with smaller value.
    vectorList.push_back(i);

    unsigned int aBegin = 25;
    unsigned int aEnd = 35; // works if 35 is replaced with 34.

    vectorList.insert(vectorList.begin() + 35, // works if 35 is
    replaced by 36, 37, 38 ...
    // but not if 35 is replaced by 34, 33, 32...
    vectorList.begin() + aBegin, vectorList.begin() + aEnd);

    return 0;
    }

    //========================

    When this is run, an error occurs during the insertion about
    incompatible vector iterators.
    At first I thought, maybe this is happening because I'm trying to
    insert into the range that I am copying...but, if the size of the
    vector is just changed from 45 elements long to 44 elements long...the
    program works! I think that is just bizarre.

    Does anybody understand what is going on here? It would be crazy to
    write code that might fail whenever it encounters a vector.insert() so
    I would like to understand the reason this code fails but when the 45
    is replaced with 44, it works.

    Any insight appreciated!

    CK
     
    , Mar 7, 2007
    #1
    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. Steve
    Replies:
    6
    Views:
    423
    Andrew Glasgow
    Feb 14, 2004
  2. Ray
    Replies:
    23
    Views:
    744
    Christian Tismer
    Dec 25, 2005
  3. Replies:
    8
    Views:
    1,940
    Csaba
    Feb 18, 2006
  4. Replies:
    9
    Views:
    416
    Alan Johnson
    Mar 8, 2007
  5. kevin

    Please enlighten me:-0 HELP Please

    kevin, Sep 1, 2003, in forum: Javascript
    Replies:
    2
    Views:
    112
    kevin
    Sep 4, 2003
Loading...

Share This Page