Home > Software engineering >  Why the vector iterator on the complains?
Why the vector iterator on the complains?

Time:11-17

# include "stdafx. H"
#include
#include
using namespace std;
Int main ()
{
//vector Vit. First define the empty vector error that is not value mistakes
Vector Vit (5, 1);
Vector : : iterator vi;

Vi=vit. The begin ();
Vit. Push_back (2);
//and it would be an error
Vi++;

system("pause");
Return 0;
}
, well, I am vs2015 current environment, on the Internet to check the iterator articles are said by + + on the point to the next element, but run by vi++ will be submitted to the Expression: vector iterator not incrementable)
Error iterator is increasing, why is this so?

CodePudding user response:

Vit. Push_back (2) and vi=vit. The begin () substituted position would not be wrong

CodePudding user response:

Question reason: because you start allocated a capacity of 5, the vector of the initial value is 1, you get the iterator memory address, then you end a value, so the compiler will handle: all copies of the original values, to find a space in the address, will copy with the new value to the new space, this time, what are you doing with the previous address iterator + +, affirmation is to collapse, operation is the address you have no legal power to give you so operation, could you also maybe in Linux platform can make up for the past,

CodePudding user response:

reference 3 floor Cain Xcy response:
question reason: because you start allocated a capacity of 5, the vector of the initial value is 1, you get the iterator memory address, then you end a value, so the compiler will handle: all copies of the original values, to find a space in the address, will copy with the new value to the new space, this time, what are you doing with the previous address iterator + +, affirmation is to collapse, operation is the address you have no legal power to give you this operation, could you also maybe in Linux platform can make up for the past,

Hello, turned out to be so, I started thinking about at the end of the additional element, won't make the memory address changes, the original is the copy to the new address, but I put this in vc6.0 run will not quote this wrong, because version 6.0 of this kind of mistake didn't check?

CodePudding user response:

reference wings of liberty, 4/f, Sai response:
Quote: refer to the third floor Cain Xcy response:
question reason: because you start allocated a capacity of 5, the vector of the initial value is 1, you get the iterator memory address, then you end a value, so the compiler will handle: all copies of the original values, to find a space in the address, the new value is copied into a new space together, this time, what are you doing with the previous address iterator + +, it must be to collapse, the operation is the address you have no legal power to give you this operation, could you also maybe in Linux platform can make up for the past,

Hello, turned out to be so, I started thinking about at the end of the additional element, won't make the memory address changes, the original is the copy to the new address, but I put this in vc6.0 run will not quote this wrong, because version 6.0 of this kind of mistake didn't check?

VC6.0 version of vector to examine also is not very perfect, each version will have a lot of bugs, Microsoft is saved to the later version to revise after the 2010 version,

CodePudding user response:

Brothers, and post it
  • Related