Given a std::vector of vertices containing some 3d integer data:
struct Vertex{
int x;
int y;
int z;
}
std::vector<Vertex> vertices = {{10, 10, 10}, {20,20,20}, {30,30,30}, {40,40,40} };
I want to calculate distances between pair of adjacent vertex:
std::vector<int> distances; // distance between adjacent vertices
I have the following for-loop:
for(int i=1; i<vertices.size(); i+=2){
int dist = calculateDistance( vertices[i], vertices[i-1]);
distances.push_back( dist );
}
Note the +=2.
Is there any STL algorithm that I can use instead of this loop? or is this even convertible to a range-based loop?
This question is different from: stackoverflow.com/q/8267806 because it wants to find distance between adjacent pairs of points.
I don't know if this solution might be suitable for you for the cons it may have if you are using the vector in another place.
You could be declaring this if in your program all the vertexes always go in pairs:
So when you need to calculate the distances you could use:
Also you wouldn't need to store the dist value into a integer. So the reduced code could be:
Please tell me if this was useful or not.