Donovan Rebbechi wrote:
On 2005-03-21, Aaron Walker <ka*****@cfl.rr.com> wrote: I want a std::map<std::string, std::string> but I don't want it
sorted by keys.
As has been asked -- why not ? Your version is just a less efficient
version of existing classes. You gain slight speedup on insertion (which you
could also get with a hash map), and trade that for vastly inferior
performance on lookup and removal.
The obvious answer to me would be, to keep the insertion order. Adding
a
new key should append that key/value pair, not insert in the middle.
Such a class could make sense in some contexts. It's not very difficult
to write. Sketch (not compiled)
template< typename K, typename V >
class associative_array {
typedef std::vector<std::pair<K,V> > vec;
vec data;
std::map< boost::ref<K>, vec::iterator > assoc;
public:
V& operator[]( K const& key]) { return assoc[key]->second; }
...
Of course, if K is small you can replace boost::ref<K> with K, the
OP wanted strings and storing them twice may be expensive. Another
solution could be to drop the map, and implement a Red-Black tree
yourself. You'd then store a node<K,V> containing two node<K,V>*s
but that is only needed if profiling shows the map is a bottleneck.
HTH,
Michiel Salters