Bilal
Bilal

Reputation: 3854

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 !

  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
                }
}

Upvotes: 0

Views: 123

Answers (1)

Olaf Dietsche
Olaf Dietsche

Reputation: 74018

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

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 (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);
    // ...
}

Upvotes: 2

Related Questions