0

When you refers to cppreference, you will find this:

Returns an iterator to the element following the last element of the container. This element acts as a placeholder; attempting to access it results in undefined behavior.

Then I run the following code:

std::set<int> s {1, 2, 3};
cout << *s.end() << endl;

The out put is: 3, why?

cong
  • 1,105
  • 1
  • 12
  • 29

1 Answers1

6

What will be returned from std::set.end() exactly in c++?

It returns:

an iterator to the element following the last element of the container.

Note that:

This element acts as a placeholder; attempting to access it results in undefined behavior.

 

The out put is: 3, why?

Because:

attempting to access it results in undefined behavior.

so it can do anything your compiler feels like doing, and returning 3 is a kind of anything, that your compiler apparently felt like doing.

user253751
  • 57,427
  • 7
  • 48
  • 90