Help with iterating

Hi there, I've finally decided to take the leap into C++ and have picked up C++ Primer (4th Edition), after a lot of review reading this seemed like one that had a lot of positive opinions.

Anyway, my problem seems embarrassingly simple but it's puzzling me. I'm trying to create an int vector of 10 elements, all starting at 0, and then simply iterate through them and change them all to 1.

Here is my code, it all compiles fine, but is not giving me the output I expect, showing me that I am misunderstanding something fundamental. I am using g++ via Code::Blocks. Also, I realize there's unnecessary headers/usings that could be simplified (which are from other exercises), but I'm trying to just go by the book before I jump to shortcuts to make sure I don't skip important information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <vector>
#include <cctype>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;

int main()
{

    vector<int> ivec(10);

    cout << "Show vector" << endl;
    for (vector<int>::iterator it = ivec.begin(); it != ivec.end(); ++it) {
        cout << ivec[*it] << " ";
    }

    cout << endl << endl << "Alter and show vector" << endl;
    for (vector<int>::iterator it = ivec.begin(); it != ivec.end(); ++it){
        *it = 1;
        cout << ivec[*it] << " ";
    }

    cout << endl << endl << "Alter and show vector (again)" << endl;
    for (vector<int>::iterator it = ivec.begin(); it != ivec.end(); ++it){
        *it = 1;
        cout << ivec[*it] << " ";
    }

    cout << endl << endl << "Done!" << endl;
    return 0;
}


This gives me the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
Show vector
0 0 0 0 0 0 0 0 0 0

Alter and show vector
0 1 1 1 1 1 1 1 1 1

Alter and show vector (again)
1 1 1 1 1 1 1 1 1 1

Done!

Process returned 0 (0x0)   execution time : 0.167 s
Press any key to continue.


What I am not understanding is why my first element on the first attempt remains at 0, while doing the exact same procedure again makes the vector as I would expect (all 1s). I want all 0s to all 1s in just one go, not this strange 0 hanging out in the mean time (which almost seems like I've printed the text before it finished iterating if that's possible?)

Anyway, I'm kind of expecting this problem to be something really simple that I've overlooked and will feel embarrassed about, but it has me stumped and I don't want to continue learning with a flawed understanding.

If someone could illuminate me on the proper way to approach this, as well as describe why it comes out incorrectly the first time but not the second, that would be really helpful for me. Thanks for your time!
If you want to show the vector in each of those lines, perhaps you should consider using *it instead of ivec[*it].

On the first output, this will always return the value of the first element (because all the elements of the vector are 0, and you're plugging that into the [ ] operator).

On the next two the value of the second element is always returned because each element pointed by it is set to 1 before it's read and this is plugged into [ ]. On the first iteration of the second loop, the first element is still 0 (hence the leading 0), however on the second iteration it is changed to 1 (hence all the following 1s).

I hope this helps.

-Albatross
In other words: use either iterators or direct access (i.e. vec[] notation), not both. *it IS the element!
Thanks so much guys! These iterators are a new beast for me, I've always been used to using bracket notation for sub-elements in higher-level scripting; I'll get a hang of it though soon enough, though!

Works just fine - thanks again, my goof is very clear now!
Topic archived. No new replies allowed.