-1

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.

6
  • You can make a range-based for loop, and then declare a variable i before the loop, and at the end of the loop, increment i. That can be one of the ways. Commented Apr 3, 2022 at 14:07
  • There are ways... Better than the basic for-loop approach, probably not. Why do you want to change that in the first place? Commented Apr 3, 2022 at 14:08
  • This seems to be the most cleanest approach. Commented Apr 3, 2022 at 14:15
  • 1
    I found this similar question: stackoverflow.com/q/8267806 and the answer seems like you can't (though things may have changed in C++17) Commented Apr 3, 2022 at 14:22
  • @Artyer That sounds good. Is it possible that i can give my own calcluateDistance() method to std::adjacent_difference ? Commented Apr 3, 2022 at 14:22

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The answer is missing the +=2
0

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]); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.