c++ - 在 C++ 中,std::set.end() 会返回什么?

标签 c++ stl

<分区>

当您引用 cppreference 时,你会发现这个:

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.

然后我运行以下代码:

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

输出是:3,为什么?

最佳答案

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

它返回:

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.

The out put is: 3, why?

因为:

attempting to access it results in undefined behavior.

因此它可以做您的编译器想做的任何事情,并且返回 3 是您的编译器显然想做的任何事情。

关于c++ - 在 C++ 中,std::set.end() 会返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42661535/

相关文章:

c++ - 如何解决 codechef ide 上的 sigabrt 错误

c++ - 检查数字是否是 C++ 中的回文

C++ 模板元编程,使用可变模板来执行结构的特定成员的操作

c++ - 插入任何 STL 集合的方法

c++ - 在 C++ 中实现 PDE 积分器的周期性边界条件

c++ - 给定一组点,找到与查询点曼哈顿距离最小的点

c++ - 正确实现删除 vector 元素

c++ - SDL2 似乎每 50 毫秒才轮询一次输入

c++ - 如何用多态实现数据局部性?

c++ - 释放 STL vector 中的对象