Home > Software engineering >  How to replace a for-loop with STL based algorithm or range based loop?
How to replace a for-loop with STL based algorithm or range based loop?

Time:04-12

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.

CodePudding user response:

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:

std::vector<std::pair<Vertex, Vertex>> vertices = { {{10, 10, 10}, {20,20,20}}, {{30,30,30}, {40,40,40}} }; 

So when you need to calculate the distances you could use:

for(auto v : vertices)   //Note that auto is in this case std::pair<Vertex, Vertex>
{
    int dist = calculateDistance(v.first, v.second);
    distances.push_back(dist);
}

Also you wouldn't need to store the dist value into a integer. So the reduced code could be:

for(auto v : vertices) distances.push_back(calculateDistance(v.first, v.second));

Please tell me if this was useful or not.

CodePudding user response:

There are no algorithms in <algorithm> that operate on pairs of elements with your access pattern. The closest are std::adjacent_find and std::adjacent difference, which both visit (most) elements twice, instead of only once.

In C 23 there will be std::ranges::views::chunk, which will allow you to make subranges of a specified size

using namespace std::ranges::views;
auto distances = vertices | chunk(2) | transform([](auto chunk){ return calculateDistance(chunk[0], chunk[1]); });
  • Related