【发布时间】:2014-09-27 14:48:25
【问题描述】:
这个完美的程序在 Visual Studio 2013 的调试模式下失败:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void main()
{
vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
for (auto iFrom = v.cbegin(), iTo = iFrom+5; iFrom != v.cend(); iFrom = iTo, iTo += 5)
cout << *max_element(iFrom, iTo) << '\n';
}
vector iterator + offset out of range 断言失败。它失败是因为iTo > v.cend(),这在这里是无害的。调试器测试没有被取消引用的迭代器的值有什么意义?
顺便说一句,我知道我可以将上面的循环重写为:
for (auto i = v.cbegin(); i != v.cend(); i += 5)
cout << *max_element(i, i+5) << '\n';
但我试图用一段更复杂的实际代码制作一个简单的例子,其中计算新的迭代器值的计算成本很高。
我也意识到可以更改 _ITERATOR_DEBUG_LEVEL 的值来影响这种行为,但它会在某些库的二进制版本中产生问题,这些库是使用默认调试设置构建的。
【问题讨论】:
标签: c++ visual-c++ stl visual-studio-debugging