0

When I cout &elem or &list separately I get their addresses. How does subtracting them give you an int?

vector<int> list;
for(auto& elem:list) { 
int i = elem;
int pos = &elem-&list[0]; // pos contains the position in the vector 

// also a &-operator overload proof alternative (thanks to ildjarn) :
// int pos = addressof(elem)-addressof(list[0]); 

}

0 Answers0