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).