Problem with Iterators

Hello guys,
i have a problem with Iterators. The exercise is to let the user input numbers in a vector. I then have to add the first and the last number, then the second and the second last number... etc. But in my for loop i get an error while i try to print the second iterator, whose job is to iterate through the vector from the back. The error message is:
Indirection requires pointer operand ('long' invalid)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  int main()
{
    
    int input;
    std::vector<int> store;
    
    while(std::cin >> input)
    {
        store.push_back(input);
    }
    
    auto vectorMid = store.begin() + (store.end() - store.begin())/2;
    
    for(auto it = store.begin(); it != vectorMid; it++)
    {
        auto it2 = store.end() - 1 - it;
        
        std::cout << *it2 << std::endl;
        //Error: Indirection requires pointer operand ('long' invalid)
    }
    
	return 0;
}
Your variable it2 isn't an iterator. It's a number (more accurately, a difference_type, but generally that's a number). An iterator minus an iterator gives a number.
Ok, thank you very much!
Hello Vivty,

Welcome to the forum.

It looks like the problem is in line 12, i.e., the math.

If "store.begin() = 0" and "store.end() = 10 then your formula would be " 0 + 10 - (0 / 2) or 0" which would end up as "0 +10 - 0". Not quite the mid point you are looking for. I think what you want is: auto vectorMid = (store.begin() + (store.end()) - ((store.begin() + store.end())/2);. And yes due to the order of precedence, see http://www.cplusplus.com/doc/tutorial/operators/#precedence , All those () are necessary. Note look at level 5 and 6.

And I think I would move line 16 to line 13 and change the for loop to end with "it++, it2--)". Since the value of "store.end()" should not change and setting the value of "it2" inside would yield the same address each time.

Now I have not tested this yet. This is what I see for now. I will load it up and give it a try though.

Hope that helps,

Andy
Hello Andy,
i fixed the problem, it2 wasn't an iterator. I guess i got that error message because C++ thought I forgot to add the "pointer syntax" (The * and adress of operator) to it2.
1
2
3
4
5
6
7
8
9
int main()
{
    int q = 45;
    int p = q; //Missing "pointer syntax"

    std::cout << *p << std::endl;
    //Error:Indirection requires pointer operand ('int' invalid)
    return 0;
}

I did a program to see if my guess was right and i really got the same error message!
I now "fixed" the problem, i guess. I think it's not the best way to do it but it works :)!
Here is the working code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    int input;
    std::vector<int> store;
    
    while(std::cin >> input)
    {        store.push_back(input);
    }
    
    auto vectorMid = store.begin() + (store.end() - store.begin())/2;
    
    for(auto it = store.begin(); it != vectorMid; it++)
    {
        auto difference = it - store.begin(); //Calculate position of the first Iterator
        auto it2 = store.end() - difference - 1; //Initialize second iterator (-1 because .end() is one off the end!)
        
        std::cout << *it + *it2 << std::endl;
    }
    
	return 0;
}

Thanks for your help guys :)!

Vivty
Hi Vivty,
I think there's still more to do here. Firstly, it looks like an infinite while loop -- you should control how many numbers you're inputting, or parse a getline. Secondly, I entered "10 20 30" and got the output "40".

I then have to add the first and the last number, then the second and the second last number... etc

So... (10+30) + (20+20)? Should we get 80 instead?

Sample code with control over number of input integers (kept your latest below that):

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
#include <iostream>
#include <vector>

int main()
{
    int input;
    std::vector<int> store;
    
    int total;
    std::cout << "How many numbers would you like to enter? ";
    std::cin >> total;
    std::cout << "Please enter " << total << " space-separated numbers: ";
    for (int x=1; x<=total; ++x)
    {
        std::cin >> input;
        store.push_back(input);
    }
    std::cout << std::endl << std::endl;
    
    auto vectorMid = store.begin() + (store.end() - store.begin())/2;
    
    for(auto it = store.begin(); it != vectorMid; it++)
    {
        auto difference = it - store.begin(); //Calculate position of the first Iterator
        auto it2 = store.end() - difference - 1; //Initialize second iterator (-1 because .end() is one off the end!)
        
        std::cout << *it + *it2 << std::endl;
    }
    
	return 0;
}


Output:
How many numbers would you like to enter?  3
Please enter 3 space-separated numbers:  10 20 30


40
Last edited on
Hello icy1,
i don't see any problem with the output. This program is intended to output the sum of the first and the last element, the second and the second last element etc. That means if you input an odd number like 3, you will only get one output, which is here: 40 (10 + 30). If i input 4 numbers:
50 20 80 30 i'll get the output: 80 100
But you're right, i should probably check if the number is odd and limit the numbers the user can input, thanks!
Next time i'll try to make the explanation more detailed, im sorry if some of you misunderstood what i wrote.

Vivty
Last edited on
Ok, so you're not summing the sums; just printing them out separately.
Even inputs are fine, but there's a problem with odd inputs. First output should be "40" for sure, but the second one you should think about. Should it do 20+20 and output 40? or should it simply output 20? Basically, it's completely ignoring the middle number.

Hope that helps ;D
Last edited on
For example, you could declare a forwards and a backwards iterator outside the loop and compare with <. After the loop is done, if they're equal, then we had an odd number of elements and you can decide what you want to do (print value once, sum it, etc):

1
2
3
4
5
6
7
8
auto it = store.begin();
auto it2 = store.end() - 1;
for(; it<it2; ++it,--it2)
{
    std::cout << *it + *it2 << std::endl;
}
if (it == it2) // Odd number of elements
    std::cout << *it << std::endl;
Last edited on
Topic archived. No new replies allowed.