Std::distance?

It seems that the standard function “distance” from the iterator library is not recognized in CINT. I want to use it to find the index of a given element of a vector:

[code]* ROOT v5.31/01 *

root [0] #include
root [1] #include
root [2] #include
root [3] std::vector v;
root [4] v.push_back(5)
root [5] v.push_back(7)
root [6] v.push_back(13)
root [7] std::vector::iterator it0 = v.begin()
root [8] std::vector::iterator it1 = std::find(it0,v.end(),7)
root [9] *it1
(const vector<int,allocator >::iterator::reference)7
root [10] int d = std::distance(it0,it1)
Error: Function distance(it0,it1) is not defined in current scope (tmpfile):1:
*** Interpreter error recovered ***[/code]

I couldn’t find a ROOT equivalent (TDistance?), is there any way to make this work in CINT?

Jean-François

It also does not seem to work in code compiled by ACLIC:

root [6] ntp6->Process("makeDCHplots.C+") Info in <TUnixSystem::ACLiC>: creating shared library /Users/jfcaron/Projects/QA/./makeDCHplots_C.so In file included from /Users/jfcaron/Projects/QA/makeDCHplots_C_ACLiC_dict.cxx:17: In file included from /Users/jfcaron/Projects/QA/makeDCHplots_C_ACLiC_dict.h:34: /Users/jfcaron/Projects/QA/./makeDCHplots.C:195:10: error: no matching function for call to 'distance' ind = std::distance(lunds.begin,std::find(lunds.begin(),lunds.end(),abs(mclund[i]))); ^~~~~~~~~~~~~ /usr/include/c++/4.2.1/bits/stl_iterator_base_funcs.h:114:5: note: candidate template ignored: deduced conflicting types for parameter '_InputIterator' ('<bound member function type>' vs. '__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >') distance(_InputIterator __first, _InputIterator __last) ^ 1 error generated. clang: error: no such file or directory: '/Users/jfcaron/Projects/QA/makeDCHplots_C_ACLiC_dict.o' Error in <ACLiC>: Compilation failed! Error in <TSelector::GetSelector>: The file makeDCHplots.C+ does not define a class named makeDCHplots.

Cheers,
Philippe.

PS. std::distance is currently not supported in CINT

Ah, sorry that the error upon compilation was a stupid one, missing brackets.

Anyways, I was able to sidestep the distance issue by using pointer arithmetic on iterators as suggested by others:

Which is apparently not generally recommended, but works in this case since “lunds” is an std::vector which is guaranteed to be contiguous in memory.

Jean-Francois