如何从向量映射中迭代向量
How to iterate a vector from a map of vectors?
我的地图是用向量构建的,我想迭代它,但我不知道怎么做!
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
}
}
注释已经为您提供了所需的所有提示。
如果我们假设it->second.nodeRefList
是一个容器(而不是迭代器(,并且行号对应于内部循环,那么内部循环看起来应该或多或少像
for(auto j = it->second.nodeRefList.begin(); j != it->second.nodeRefList.end(); ++j)
; // do something with node iterator (j)
更好的是,使用基于范围的循环
for (auto &node : it->second.nodeRefList)
; // do something with node
要使用连续元素计算距离,可以使用两个以步调一致的移动的迭代器
auto &nodes = it->second.nodeRefList;
for (auto i1 = nodes.begin(), i2 = i1 + 1; i2 != nodes.end(); ++i1, ++i2) {
auto dist = euclidean_distance(*i1, *i2);
// ...
}
相关文章:
- std::向量迭代器和调整大小/保留的奇怪/有趣行为
- 如何取消引用向量迭代器
- Map中的(字符串的)向量迭代器
- C++将向量迭代器转换为索引
- yaml-cpp到std::向量迭代的怪异行为
- 使用索引与迭代器将向量迭代到倒数第二个元素
- 无法取消引用超出范围的向量迭代器 - 有什么问题?
- 向量迭代器不兼容的错误,用于保存另一个向量的迭代器的向量
- C++向量迭代器nth_element编译错误
- 无法查找值初始化的向量迭代器?
- C++向量迭代:常量 vs. 常量自动 vs. 无常量
- 向量迭代编译为非常不同的指令
- STD ::向量迭代器类型和允许的操作
- 如何通过尺寸未知的2D向量迭代
- 向量迭代器 < 或 !=
- 如何通过共享_ptr向量迭代
- 如何通过向量迭代作为指针
- 通过向量迭代,而其他线程可能会对其进行修改
- 我如何通过C 中的字符串向量迭代
- 向量迭代器在新对象的 push_back() 之后不兼容