是否可以使用超出范围的 RA 迭代器?

发布于 2024-08-11 09:44:07 字数 564 浏览 1 评论 0原文

考虑以下代码:

typedef std::vector<int> cont_t; // Any container with RA-iterators
typedef cont_t::const_iterator citer_t; // Random access iterator

cont_t v(100);
const int start = 15; // start > 0.
citer_t it = v.begin() - start; // Do not use *it

int a1 = 20, b1 = 30; // a1, b1 >= start
int a2 = 30, b2 = 40; // a2, b2 >= start

int x = std::min_element(it + a1, it + b1); // 
int y = std::min_element(it + a2, it + b2); //
int z = std::min_element(it + 15, it + 25); //
...

是否可以使用超出范围的随机访问迭代器it

Consider the following code:

typedef std::vector<int> cont_t; // Any container with RA-iterators
typedef cont_t::const_iterator citer_t; // Random access iterator

cont_t v(100);
const int start = 15; // start > 0.
citer_t it = v.begin() - start; // Do not use *it

int a1 = 20, b1 = 30; // a1, b1 >= start
int a2 = 30, b2 = 40; // a2, b2 >= start

int x = std::min_element(it + a1, it + b1); // 
int y = std::min_element(it + a2, it + b2); //
int z = std::min_element(it + 15, it + 25); //
...

Is it possible to use the random access iterator it out of range?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

通知家属抬走 2024-08-18 09:44:07

当然可以编写尝试使用超出范围的迭代器的代码。运行代码将给出未定义的行为。根据库的实现,它可能会抛出异常、访问内存的随机位、触发保护故障或在 CPU 中引发热核爆炸。

It's certainly possible to write code that trys to use an out-of-range iterator. Running the code will give undefined behaviour. Depending on the library implementation, it may throw an exception, access random bits of memory, trigger a protection fault, or initiate a thermonuclear explosion in your CPU.

半山落雨半山空 2024-08-18 09:44:07

您将根据 C++ 标准 24.1.5 表 76 在此处获得断言条件。

You'll get assertion condition here according to C++ Standard 24.1.5 Table 76.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文