boost::multi_index iterators and std::distance
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. 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). Is there a work around for this?
Hi Jeffrey,
----- Mensaje original -----
De: Jeffrey Holle
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
I've rewritten my test program, removing all but the parts that test distance and I now see that its working as expected. There still is an issue about my previous programs strange behavior, but its my problem and not something with multi_index functionally. Thanks for your response.
participants (2)
-
"JOAQUIN LOPEZ MU?Z"
-
Jeffrey Holle