我的项目中有以下代码:
std::cout << "Without iterator:\n";
std::vector<RectangleCluster*> rc = p->getInnerRegionPolygons().at(0)->getClusters();
for (int i = 0; i < rc.size(); i++)
std::cout << "Cluster " << rc.at(i)->getId() << ": start = " << rc.at(i)->getOverlapAtStart() << ", end = " << rc.at(i)->getOverlapAtEnd() << std::endl;
std::cout << "\n\nWith iterator:\n";
for (std::vector<RectangleCluster*>::iterator ci = p->getInnerRegionPolygons().at(0)->getClusters().begin(); ci != p->getInnerRegionPolygons().at(0)->getClusters().end(); ++ci)
std::cout << "Cluster " << (*ci)->getId() << ": start = " << (*ci)->getOverlapAtStart() << ", end = " << (*ci)->getOverlapAtEnd() << std::endl;
控制台输出如下所示:
没有迭代器:
群集0:开始= 65.4238,结束= 64.9811
集群1:开始= 64.9811,结束= 17.9983
集群2:开始= 17.9983,结束= 17.9983 ......
使用迭代器:
Cluster 34026352:start = 1.68113e-316,end = 1.68113e-316
集群1:开始= 64.9811,结束= 17.9983
集群2:开始= 17.9983,结束= 17.9983 ......
“......”之后的输出也相等。为什么没有迭代器的代码给我“Cluster 0:start = 65.4238,end = 64.9811”,迭代器代码为“Cluster 34026352:start = 1.68113e-316,end = 1.68113e-316”?
祝你好运 的Gernot
答案 0 :(得分:3)
请使用以下迭代器重写代码:
for (std::vector<RectangleCluster*>::iterator ci = rc.begin(); ci != rc.end(); ++ci)
std::cout << "Cluster " << (*ci)->getId() << ": start = " << (*ci)->getOverlapAtStart() << ", end = " << (*ci)->getOverlapAtEnd() << std::endl;
我同意阿伍德兰的评论。很可能是p-> getInnerRegionPolygons()。at(0) - &gt; getClusters()每次调用都会返回新的向量。并且比较来自(...)运算符内部的不同容器的迭代器是不正确的。