Boost logo

Boost Users :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2007-08-30 09:09:47


Hi Jeffrey,

----- Mensaje original -----
De: Jeffrey Holle <jeffreyholle_at_[hidden]>
Fecha: Jueves, Agosto 30, 2007 5:06 am
Asunto: [Boost-users] boost::multi_index iterators and std::distance
Para: boost-users_at_[hidden]

> In experimenting with boost::multi_index, I find that std::distance
> does not work properly multi_index iterators.
>
> My multi_index as a single ordered index for now, and I've tried:
> C cs;
> Index& index = cs.get<0>();
> ... populate cs
> Index::iterator iter=index.find(7);
> cout << "distance is \"" << distance(cs.begin(),iter) << "\"" <<
> endl;
> cout << "distance is \"" << distance(index.begin(),iter) <<
> "\"" endl;
>
> Both outputs print that the distance is 0, and should be 7.

Unfortunately I'm away from an environment where I can directly
test your snippet, so the following is mere speculation. Excuse
my challenging your assumptions, but are you sure the distance
really should be 7, that is, have you verified that *there are
seven elements whose keys are less than 7*? As it looks,
std::distance(begin(),iter) being 0 means that the element with
key 7 is the first element of the index, that is, all the other
elements have keys greater than 7. If this is not the case then
there's certainly a bug, but I'd be grateful if you can double
check this.

> Note both output lines compile and I've also tried using operator-
> and operator+ with the iterators and found that they don't
> exist (as expected).

Correct, as iterators for ordered indices are not random access.

> Is there a work around for this?

As soon as I can run the snippet myself I'll provide you with
some more feedback. In the meantime I'd be extremely grateful if
you can check the questions I pose above.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net