You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stdcxx.apache.org by Craig Chariton <ch...@roguewave.com> on 2006/05/12 22:56:25 UTC

Problem with vectors and insert

There is an interesting problem that occurs with vectors.  Here is the
sample code:

 

#include <iostream>   // for cout, endl

#include <vector>     // for vector

 

int main ()

{

 

  std::vector<int> a;

  a.push_back(1);

  a.push_back(2);

  a.push_back(3);

  a.push_back(4);

  

  std::vector<int> b;

  b.push_back(0);

  

  a.insert(a.begin(), b.begin(), b.end());

 

  for( std::vector<int>::iterator i = a.begin(); i != a.end(); ++i)

    std::cout << (*i) << ",";

  std::cout << std::endl;

 

  return 0;

}

 

The output is 0,2,3,1,2, instead of 0,1,2,3,4, .

 

I tried some other combinations as follows:

 

a = 1, b = 0  -  works, get 0,1

a = 1,2, b = 0  -  works, get 0,1,2

a = 1,2,3, b = 0  -  fails, get 0,2,1,2

a = 1,2,3, b = 0, 5  -  fails, get 0,5,3,2,3

a = 1,2, b = 0,5  -  works, get 0,5,1,2

a = 1,2,3,4, b = 0,5  -  works, get 0,5,1,2,3,4

a = 1,2,3,4,5, b = 0,5  -  fails, get 0,5,3,1,2,3,5

 

It appears to be consistent across platforms and compilers.

 

I have not been able to find any references to such a problem with vectors
on the lists or in JIRA.  Has this problem been recognized and solved?

 

Craig Chariton



 


Re: Problem with vectors and insert

Posted by Martin Sebor <se...@roguewave.com>.
Craig Chariton wrote:
> There is an interesting problem that occurs with vectors.  Here is the
> sample code:
[...]
> I have not been able to find any references to such a problem with
> vectors
> on the lists or in JIRA.  Has this problem been recognized and solved?

AFAIK, it's not a known issue. Could you please open an issue
in Jira and cross-refernce this thread? Thanks for pointing it
out!

Martin