MMM - 10 months ago 57

C++ Question

So I currently have a vector of my custom struct.

Each element in my vector is a graph point (x,y). Given an index in my vector, I need to be able the euclidean distance between the index given and all the other points. I already know how to calculate euclidean distance, **what I want to know is how I can check any element in my vector against all the others without rotating.**

For example in my vector if I have:

(3,5) , (4,6) , (7,8) , (5,3)

as an example and I was given index 2 (7,8) I need to be able to calculate the distance between (7,8) and the other 3 points because I'm interested in finding the shortest path. I thought of using the std::rotate function and move (7,8) to the front but I don't want to mess with my vector. **Is there a way I can accomplish being given an index in my vector to an element and comparing it to all other elements in the vector without changing the vector so I can do my computations? Thank you**

Answer Source

```
for(const auto& i : yourvector)
compare(i, yourvector[foo]);
```

That ought to do it if looping through the vector is OK.

Else you can `sort`

the vector to put the closest elements first: http://www.cplusplus.com/reference/algorithm/sort/