   std::vector index & iterator
  Hi guys.I wonder how would get an index of a vector if I know an iterator of a vector, suppose I do not have duplicated values in my vector.std::vector<float> v;v.push_back(0.03);v.push_back(0.04);v.push_back(0.05);v.push_back(0.06);Say I know an iterator "iterator" of the above vector. How would i find the index of that iterator?All I can think is to do the followingfor(int i = 0; i < v.size(); i ++)   if(*iterator == v) break;now... i is the result
  You can use the difference operator:iterator - v.begin();Sample code:#include <vector>#include <iostream>using namespace std;int main() {int vals[] = { 6, 3, 7, 10, 3, 8, 5 };vector<int> vec( vals, vals + sizeof(vals)/sizeof(vals[0]) );for ( vector<int>::iterator it = vec.begin(); it != vec.end(); ++it )cout << it - vec.begin() << ' ';}Outputs:0 1 2 3 4 5 6   ;  or#include <iterator>std::distance<v.begin(), iterator>   ;Quote:Original post by Rattrapor#include <iterator>std::distance<v.begin(), iterator>the iterator differences are the same for all the types? i.e.  vector<float> vector<int> vector<double> ?   ;  The two input iterators have to be from the same source.  Other than that, it should find the distance between any two iterators.   ;  Why do you think you need the index?   