lundi 23 juillet 2018

How do you use std::distance to find the array index of a pointer to an std::array element?

Let's say I have a std::array of some object and create a pointer to one of the objects.

std::array<Object, 100> my_array;

Object* ptr_object = &my_array[50]; 

So assuming I don't know the index referred to by ptr_object, how would I go about and back-track to find this index in C++11?

I've come across some readings that suggest std::distance might help, however, my attempt of

std::distance(my_array, ptr_object);

throws an error stating that "no matching overloaded function found".

Aucun commentaire:

Enregistrer un commentaire