Odd values printed when dereferencing the end iterator of a vector

后端 未结 4 1483
[愿得一人]
[愿得一人] 2021-01-06 13:33

I have a vector storing {1,2,3,4,5}. I tried to print *(vec.end()) and got back the result 6. I don\'t know how to explain this. Similarly, calling vec.fi

4条回答
  •  别那么骄傲
    2021-01-06 14:13

    This line invokes undefined behavior:

    std::cout << *(set1.end()) << std::endl;

    It is undefined behavior to dereference the end() iterator. Thus anything can be expected.

提交回复
热议问题