Shiv Pratap Singh
Shiv Pratap Singh

Reputation: 118

looping with iterator in a vector

I was just writing a test program on iterators in vector, In the begining I had just created a vector and initialize it with a series of numbers 1-10.

After that I had created a iterator "myIterator" and a const iterator "iter". I had used iter to display contents of the vector.

Later on I had assigned "myIterator" to "anotherVector.begin()". So they are pointing to same thing.

checked by

//cout << /* *myIterator << */"\t" << *(anotherVector.begin()) << endl;

so in the second iterator loop i just replaced "anotherVector.begin()" with myIterator.

But that produced a different output.

code is:

    vector<int> anotherVector;

for(int i = 0; i < 10; i++) {
    intVector.push_back(i + 1);
    cout << anotherVector[i] << endl;
}

    cout << "anotherVector" << endl;

//*************************************
//Iterators

cout << "Iterators" << endl;

vector<int>::iterator myIterator;
vector<int>::const_iterator iter;

for(iter = anotherVector.begin(); iter != anotherVector.end(); ++iter) {
    cout << *iter << endl;
}

cout << "Another insertion" << endl;

myIterator = anotherVector.begin();

//cout << /* *myIterator << */"\t" << *(anotherVector.begin()) << endl;

myIterator[5] = 255;
anotherVector.insert(anotherVector.begin(),200);

//for(iter = myIterator; iter != anotherVector.end(); ++iter) {
    //cout << *iter << endl;
//}

for(iter = anotherVector.begin(); iter != anotherVector.end(); ++iter) {
    cout << *iter << endl;
}

Output using

for(iter = anotherVector.begin(); iter != anotherVector.end(); ++iter) {
    cout << *iter << endl;
}

gives:

    Iterators
    1
    2
    3   
    4
    5
    6
    7
    8
    9
    10
    Another insertion
    200
    1
    2
    3
    4
    5
    255
    7
    8
    9
    10

and Output using

for(iter = myIterator; iter != anotherVector.end(); ++iter) {
    cout << *iter << endl;
}

gives:

    Iterators
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Another insertion
    0
    0
    3
    4
    5
    255
    7
    8
    9
    10
    81
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    0
    0
    0
    0
    0
    0
    0
    0
    97
    0
    200
    1
    2
    3
    4
    5
    255
    7
    8
    9
    10

Why there is so much difference if they are just pointing the same address.

Upvotes: 0

Views: 76

Answers (2)

Shiv Pratap Singh
Shiv Pratap Singh

Reputation: 118

I just found my error but you can check the variation in the location of the address of the iterators.

myIterator = anotherVector.begin();

    cout << "test line\t" << &(*myIterator) << "\t" << &(*(anotherVector.begin())) << endl;

    //myIterator[5] = 255;
    anotherVector.insert(anotherVector.begin(),200);

    cout << "test line\t" << &(*myIterator) << "\t" << &(*(anotherVector.begin())) << endl;

This gives the output:

before insertion

test line   0x92f070    0x92f070

after insertion

test line   0x92f070    0x92f0f0

output may vary depending on the machine.

Upvotes: 0

md5i
md5i

Reputation: 3083

After your insert, myIterator is no longer necessarily valid. This is because insertion into a std::vector can cause a vector reallocation, and as such the addresses pointed to by prior iterators may not be pointing into the address space of the reallocated vector.

Upvotes: 3

Related Questions