I am trying to access the element of a vector using the iterator. But I get strange outputs.
std::vector ivec{ 7, 6 , 8, 9} ;
std::vector
For your first example, std::vector
points to the theoretical element after the actual last element, so dereferencing it does not make sense. It is primarily used for checking when you have got past the end of the vector in a loop.
For your second example, the results are as you would expect:
cout << *(beg++) ; //increments beg after dereferencing
cout << *(++beg) ; //increments beg before dereferencing
cout << *(beg+=1) ; //dereferences the result of adding one to beg