0

Given a vector

vector<classX *> myVec;

how to return the index i of one of its elements as in the following function;

size_t nearestElement(classY const& p){
     size_t i(0);
     double d = distance(myVec[i]->position(), p);
     for (auto const& element : myVec){
         if(distance(element->position(), p) < d){
            i = ???; // the index of the current element
         }
     return i;
     }
}

where position() is a function defined in classX and distance is not the std::distance function, but a function I defined my self.

Gamba Osaca
  • 307
  • 1
  • 4
  • 17
  • You may be thinking about this too much; you're looping through the *indexes* - so you already have what you need – MrDuk Apr 22 '14 at 14:33
  • Use an index loop instead of the range loop and the problem will more or less solve itself. – molbdnilo Apr 22 '14 at 14:38
  • yes it's easy like that, but isn't there anyway to return the index using the implementation above ? – Gamba Osaca Apr 22 '14 at 14:41
  • you can find more on this here (actually this SO is duplicate): http://stackoverflow.com/questions/10962290/find-position-of-element-in-c11-range-based-for-loop – marcinj Apr 22 '14 at 14:45

2 Answers2

2

Change range based for to regular for, or add indexing variable to your current for:

int index = 0;
for (auto const& element : myVec){          
     if(distance(element->position(), p) < d){
        i = index; // the index of the current element
     }
     index++
...
marcinj
  • 48,511
  • 9
  • 79
  • 100
1

&element - &myVec[0] should do the trick for container with continuous data (as std::vector).

if your distance function is cheap, you may rewrite your function like this:

size_t nearestElement(classY const& p)
{
    auto it = std::min_element(myVec.begin(), myVec.end(),
        [&p](const classX* lhs, const classX* rhs) {
            return distance(lhs->position(), p) < distance(rhs->position(), p);
        });
    return it - myVec.begin(); // Will return 0 if myVec is empty.
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302