-1

Why does the iterator pointing to the beginning of a list output the second value? Why does a.begin()++ leave begin() ahead and is there a better implementation?

#include <iostream>
#include <list>
using namespace std;
//3,2,1
int main() {
    list<int> a;
    a.insert(a.begin(),1);              
    cout << *(a.begin()) << endl;
    a.insert(a.begin(),3);
    cout << *a.begin()<< endl;
    a.insert(a.begin()++,2);
    list<int>::iterator iterator = a.begin();
    iterator++;
    cout << *iterator << endl;
        return 0;
}

My output:

1
3
3

Expected output:

1
3
2

Edit: "Because you put 2 at the start of the list. Remember that a.begin()++ is doing post-incrementing ie, it increments after all other operations. Try your code with ++a.begin() and see if it does what you expect"- @Ben

Typographic error, thanks Ben.

  • 1
    I get your expected output using both gcc and VS compilers. – Anon Mail Dec 03 '15 at 18:32
  • That's the behaviour I'd expect if you used `++it` instead of `it++`, i.e. prefix increment operator. Maybe there's a bug in the implementation of the postfix operator? What is `distance(a.begin(), a.begin()++)`? – Ulrich Eckhardt Dec 03 '15 at 18:32
  • @ateneaMinerva If it is indeed so then it can be a bug of the library. – Vlad from Moscow Dec 03 '15 at 18:32
  • Just wondering, you are not using the STL, but the C++ standardlibrary supplied with your compiler, right? Which one and which version, btw? – Ulrich Eckhardt Dec 03 '15 at 18:33
  • 1
    Wait: Your topic says "output contains 2" and that's what you claim as expected output!? Which one is right? – Ulrich Eckhardt Dec 03 '15 at 18:37
  • This is overall very confusing .. your code does exactly what it is supposed to. I suspect that you think that `a.begin()++` changes where the start of the list is. It does not .... – Fantastic Mr Fox Dec 03 '15 at 18:39
  • 1
    [In your last edit](http://stackoverflow.com/revisions/34073453/3), it is clear your output is as it should be, since you have advanced `iterator` past the element containing `2`. I am voting to close the question. – jxh Dec 03 '15 at 18:39
  • @UlrichEckhardt I use clang-700.1.76 but the main problem is the code was wrong, now I have edited it. The question is why a.begin()++ doesn't show 2. – ateneaMinerva Dec 03 '15 at 18:41
  • @ateneaMinerva Because you put 2 at the start of the list. Remember that `a.begin()++` is doing post-incrementing ie, it increments after all other operations. Try your code with `++a.begin()` and see if it does what you expect. – Fantastic Mr Fox Dec 03 '15 at 18:42
  • @Ben, Ah, if that was the intent, the question was very unclear. – jxh Dec 03 '15 at 18:44
  • @Ben HOLY S**T I didn't remember the difference between post-increment and pre-increment, this is the answer I needed, thank you all. – ateneaMinerva Dec 03 '15 at 18:45
  • Ok then this should absolutely be closed as simple typographic error. Also @jxh ... looks like i am a mind reader:) – Fantastic Mr Fox Dec 03 '15 at 18:46

2 Answers2

5

The code is fine:

#include <iostream>
#include <list>
using namespace std;
//3,2,1
int main() {
    list<int> a;
    a.insert(a.begin(),1);
    cout << *(a.begin()) << endl;
    a.insert(a.begin(),3);
    cout << *a.begin()<< endl;
    a.insert(a.begin()++,2);
    list<int>::iterator iterator = a.begin();
    cout << *iterator << endl;
    return 0;
}

Output:

1
3
2

Check at Ideone too.

Alex
  • 8,461
  • 6
  • 37
  • 49
gsamaras
  • 71,951
  • 46
  • 188
  • 305
1

Looks like this was simply forgetting that a.insert(a.begin()++,2); is equivalent to the a.insert(a.begin(), 2) in this case. This is because a post-increment will add 2 to the beginning of the list then increment the iterator. If you want your expected output then you will need to use the pre-increment operator. ie:

a.insert(++a.begin(), 2)
Community
  • 1
  • 1
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175