0

I'm new to vectors and iterators. How come the iterator in the second for-loop does NOT start at index 0?

int main() {
    PS1Solution instance;
    std::vector<int> result;

    std::vector<int> testCase = {2, 7, 11, 15};
    int target = 9;

    result = instance.twoSum(testCase, target);

    for (auto it = result.begin(); it != result.end(); it++)
        printf("%d\n", result[*it]);

    testCase.clear();
    result.clear();

    testCase = {3, 2, 4};
    target = 6;

    result = instance.twoSum(testCase, target);

    for (auto it = result.begin(); it != result.end(); it++) // for (auto& val : result) also doesn't work
        printf("%d\n", result[*it]);

    return 0;
}

The range-for loop doesn't work either. Nor does: for (auto it = &*result[0]; ...) If necessary, I could post my implementation of twoSum. Though, it's pretty simple: it uses a simple nested for-loop (indexed, NOT iterator, since I need the indices).

  • Oh, sorry. Let me change the code real quick. I was messing around and forgot to change everything back before copying/pasting. – Paul Clauss Mar 16 '22 at 01:33
  • Everything should be normal now. The second for loop should match the first one exactly. That's why I'm confused, since the first one works perfectly, and the second one fails by starting one index ahead, printing a number past the vectors memory. – Paul Clauss Mar 16 '22 at 01:35
  • 2
    On the first iteration, `*it` is the value at the first position, it is not necessarily zero. `result[*it]` will look at the value of the first position, use it as an index and return the value at *that* position. Then the second iteration will look at the second value and return the value at *that* position, etc. If the first loop happens to print the vector in order, then that vector must contain `{0, 1, 2, ... }`. I don't know what `twoSum` does so I can't speak on why the two loops do different things. To print the vector you should print `*it` not `result[*it]`. – François Andrieux Mar 16 '22 at 01:58
  • For example, the first value in `result` is 2, the first loop iteration will print `result[2]`. – François Andrieux Mar 16 '22 at 01:59
  • You're right in that the first vector contains {0, 1, 2, ...}. Is there some reference for iterators you could provide for me? Otherwise, I believe you've solved my problem. – Paul Clauss Mar 16 '22 at 02:03
  • 1
    See [What is an iterator in general?](https://stackoverflow.com/questions/51586495/what-is-an-iterator-in-general) – François Andrieux Mar 16 '22 at 02:30

1 Answers1

2

An Iterator is not an index.

An iterator acts like a pointer to a specific element. Dereferencing an iterator gives you the value it refers to, not the index of the value. So, using result[*it] is wrong, it should be just *it by itself, eg:

for (auto it = result.begin(); it != result.end(); it++)
    printf("%d\n", *it);

A range-for loop wraps this logic internally for you. The loop variable is the dereferenced value, eg:

for (auto& val : result)
    printf("%d\n", val);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770