J
John Harrison
Chris Dearlove said:John Harrison ([email protected]) wrote:
: Well believe it or not I had good reasons for having a const member.
: Specifically I needed a vector of map<T, U>::value_type. And the C++
: standard defines map<T, U>::value_type as std:air<const T, U>. Note the
: const.
OK. Now are those pairs still in a map, or have they been removed
from one? (If the answer is some of each I give up.) In the former
case is there a reason not to have a vector not of the pairs but
of iterators to the pairs? In the latter case can you not work
with a vector of non-const pairs converted from the map value type?
(I'm working from the position that unpleasant although these options
may be compared to what you really want, they aren't as unpleasant
as reinventing vector.)
What I'm actually trying to do is develop a general data structure which I'm
calling IndexedMap (add IndexedSet, IndexedMultiMap, and IndexedMultiSet).
This will be very similar to map etc. but the iterators will be random
access and reflect not the ordering of the keys but the order in which the
values were added to the set. A sort of cross between a map and a vector.
You can do it provided you don't mind deletion being O(n) instead of O(lg n)
as it is with map, insert and find remain O(lg n).
My first attempt was something similar to what you suggest, basically
combining a map with a vector and storing links between the two. One thing
bothered my about this however which was that as far as I can tell you need
to store two allocators, one in the map and one in the vector. If the user
wants to use a stateful allocator then they aren't going to be happy.
Then I realised that you could implement a red-black tree algorithm where
all the nodes are stored in a vector and everything would fall into place.
That was when I found out about the assignable requirement of vectors.
I've implemented my vector replacement, its called LightweightVector, didn't
take too long, it has everything that doesn't require you to assign elements
(push_back and pop_back for instance, but no insert or erase), even whole
vector assignment, which is done by copy and swap. As a nice bonus you get
strong exception safety as well. I think I may be using it more often.
john