2

In this code I try to move the iterator by 10 elements.

   #include <iostream>
    #include <string>
    #include <vector>
    int main()
    {
        using namespace std;
       vector<int> v(20);
       auto mid = v.begin() + 10;
        cout<<mid;


    }

On running this code, I get the error mentioned in the title. I'm a beginner. I experience this error in almost every program I write. Where am I going wrong?

Slay
  • 221
  • 1
  • 5
  • 12

1 Answers1

3

An iterator "points" to an element, what you want to be doing is:

cout << *mid;

You have to "dereference" the iterator to print what it points to. Trying to print it directly gives you the error you mentioned.

Edit: here's a little demo:

#include <iostream>
#include <vector>

int main(int argc, char* argv[])
{
    std::vector<int> numbers;
    numbers.push_back(4);
    numbers.push_back(3);
    numbers.push_back(2);

    auto beg = numbers.begin();
    auto mid = numbers.begin() + 1;
    std::cout << *beg << std::endl;
    std::cout << (beg < mid) << std::endl;      // True because beg (index 0) points to an element earlier than mid (index 1)
    std::cout << (*beg < *mid) << std::endl;    // False because the element pointed-to by beg (4) is bigger than the one pointed-to by mid (3)

    return 0;
}

Output The first line shows 4 which is the value of the first element! The second line shows 1 (all non-zero values mean true) and the last line shows 0 (zero is the only value that means false).

Borgleader
  • 15,826
  • 5
  • 46
  • 62
  • Okay, thanks! But when using it in an if statement, like if(it – Slay Jul 01 '13 at 07:07
  • Also, shouldn't this program giving me an output of 11? It gives me 0 – Slay Jul 01 '13 at 07:10
  • No, I believe your vector contains twenty 0s. – Borgleader Jul 01 '13 at 07:16
  • 1
    @user2530836 time to read [a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – juanchopanza Jul 01 '13 at 07:17
  • @Borgleader What output does auto mid = numbers.begin() + 1; give? Also, isn't the element that *beg point to less that *mid? – Slay Jul 01 '13 at 07:22
  • @juanchopanza, I'm reading c++ primer 5th edition. Can you recommend a better book, or is it better to continue with this? – Slay Jul 01 '13 at 07:22
  • @user2530836 The vector contains 3 numbers: 4,3,2 (in that order), so beg point to 4 and mid points to 3. The reason being that I assigned mid to be the begin iterator (the one that points to the first element, in this case 4) incremented by one (which means it now points to the second element: 3). – Borgleader Jul 01 '13 at 07:25
  • @user2530836 You continue with that, and maybe complement it with "The C++ Programming Language" and "The C++ standard Library". Also, [cpreference.com](http://en.cppreference.com/w/cpp) is a pretty useful reference. – juanchopanza Jul 01 '13 at 07:26
  • @juanchopanza Will it be fine to use 2-3 books at once? I'm a very beginner using my summer vacations to learn programming. isn't "The C++ Programming language" for advanced readers? – Slay Jul 01 '13 at 07:27
  • @user2530836 "The C++ programming language" assumes programming knowledge, isn't a barrel of laughs, but I would say it is essential reading. But initially, you can use it (and "The C++ Standard Library") as reference backups. Those two books will teach you about the language ant the library, but not necessarily how to code well in C++. But note that this is a subjective advice from my part. Others may have different opinions. – juanchopanza Jul 01 '13 at 07:31
  • @juanchopanza _The C++ Programming Language_ is a bit heavy going for a beginner. I'd recommend _Programming -- Principles and Practice Using C++_ to start with. The _C++ Primer_ is also highly rated --- which one is better probably depends on you. (And to be fair, the error message is simply horrible---it bears no relation to the actual problem.) – James Kanze Jul 01 '13 at 09:07
  • @JamesKanze, what do you think about The C++ Standard Library 2nd edition? It's available in a library nearby. You think it's worth giving it a try, considering I'm a beginner? – Slay Jul 01 '13 at 09:45
  • 1
    @user2530836 I've not seen it, but the 1st edition was excellent, Jossutis is an excellent author in general, and it is highly rated. – James Kanze Jul 01 '13 at 10:05