Bizzare std::distance output
Bizzare std::distance output
我直接从我正在处理的一些代码中取出了这个小片段:
KeyIter it = timeline_.lowerBound( frame );
if ( timeline_.isKeyAtFrame( frame ) ) {
++it;
}
KeyIter it1 = it - 1;
cout << "dist1: " << std::distance( timeline_.begin(), it1 ) << endl;
while ( ignore.contains( it1.key() ) ) {
cout << "dist2: " << std::distance( timeline_.begin(), it1 - 1 ) << endl;
if ( std::distance( timeline_.begin(), --it1 ) < 0 ) {
break;
}
}
cout << "dist3: " << std::distance( timeline_.begin(), it1 ) << endl;
它给出以下输出:
dist1: 0
dist2: 2
dist3: 2
ignore
是一个QSet<int>
,it1
是 timeline_
的迭代器(它是键为 int
的映射类型)。 如您所见it1
从开头开始(这是正确的),然后控制进入 while 循环,其中迭代器向后移动一个;但 std::d istance 不是 -1,而是 2! 中间发生的所有事情都是使用密钥的副本来检查QSet
是否包含相同的int
。
使用调试器,我可以确认timeline_
在两个dist#
输出之间不会更改(无论如何,此时代码中只有一个线程正在运行)。
谁能明白为什么std::distance
会给出这个输出?
我不确定Qt在这方面的行为,但在标准库容器中,获取范围之外的迭代器[container.begin(),container.end()]
是未定义的行为。 我认为在Qt中也是如此,尽管我不确定。 但是,即使不是,非随机访问迭代器上的std::distance
行为也是计算从第一个迭代器到最后一个迭代器所需的增量数,因此:
std::distance(x,y)
其中y
在x
之前,是未定义的行为。
相关文章:
- ( [ ]( ){ output;}); 是什么意思?
- 这句话是什么意思 - " vector<long long> distance(n, std::numeric_limits<long long>::max()); "?
- diff: output.txt: 没有这样的文件或目录
- 从生成文件编译错误:"Unable to open output file" ..."No such file or directory"
- 'Cannot output file filename.exe Permission denied' C++ 代码::块中的错误
- 如何修复"(Song Title) is not recognized in output screen"
- C++ library output
- 我需要用c ++ 98重写c ++ 11代码,c ++ 11正在使用lambda函数[&](const Output & o
- gradle see output for model.components.binaries.all
- 我在下面的代码上收到错误"Output Limit Exceeded"
- Output UTF-8 (u8) std::string
- 在 Eclipse.It 中运行C++代码时出错,'Your program cannot output..'
- 为什么我会得到"You must feed a value for placeholder tensor 'output' with dtype int64"?
- Metis - Block output
- 如何在C++中调试"cannot open output file allocating memory.exe: Permission denied"?
- GNU make 3.81 - 如何做'--output-sync=target'
- Sampson Distance using opencv, c++
- 将函数结果作为"output parameter"返回是什么意思?
- C++:在 2D 数组中的 2 个坐标之间"Distance"
- Bizzare std::distance output