[英]How to iterate a vector from a map of vectors?
My map built from vectors, and I want iterate it, but I don't know how to do it !我的 map 从向量构建,我想迭代它,但我不知道该怎么做!
WayMap::iterator it;
for ( it = MyWayMap.begin(); it != MyWayMap.end(); it++ )
// Loop the Whole Way Map
{
for(it->second.nodeRefList.begin();it->second.nodeRefList != it->second.nodeRefList.rbegin()-1;it->second.nodeRefList++);
// Loop The Whole Nodes of Each way
}
}
The comments give you all the hints you need already.评论为您提供了您需要的所有提示。
If we assume, that it->second.nodeRefList
is a container (and not an iterator) and the line numbers correspond to the inner loop, the inner loop should look more or less like如果我们假设it->second.nodeRefList
是一个容器(而不是迭代器)并且行号对应于内部循环,那么内部循环应该或多或少像
for(auto j = it->second.nodeRefList.begin(); j != it->second.nodeRefList.end(); ++j)
; // do something with node iterator (j)
Better yet, use a Range-based for loop更好的是,使用基于范围的 for 循环
for (auto &node : it->second.nodeRefList)
; // do something with node
To calculate the distance using consecutive elements, you could use two iterators moving in lockstep要使用连续元素计算距离,您可以使用两个同步移动的迭代器
auto &nodes = it->second.nodeRefList;
for (auto i1 = nodes.begin(), i2 = i1 + 1; i2 != nodes.end(); ++i1, ++i2) {
auto dist = euclidean_distance(*i1, *i2);
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.