是否可以使用超出范围的 RA 迭代器?
考虑以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然可以编写尝试使用超出范围的迭代器的代码。运行代码将给出未定义的行为。根据库的实现,它可能会抛出异常、访问内存的随机位、触发保护故障或在 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.
您将根据 C++ 标准 24.1.5 表 76 在此处获得断言条件。
You'll get assertion condition here according to C++ Standard 24.1.5 Table 76.