Quello che si desidera (senza ricorrere a Boost) è quello che io chiamo un "hash ordinato", che è essenzialmente un mashup di un hash e una lista collegata con chiavi stringa o numeri interi (o entrambi allo stesso tempo). Un hash ordinato mantiene l'ordine degli elementi durante l'iterazione con la prestazione assoluta di un hash.
Ho assemblato una libreria di snippet C++ relativamente recente che riempie ciò che vedo come buchi nel linguaggio C++ per gli sviluppatori di librerie C++. Vai qui:
https://github.com/cubiclesoft/cross-platform-cpp
Grab:
templates/detachable_ordered_hash.cpp
templates/detachable_ordered_hash.h
templates/detachable_ordered_hash_util.h
Se i dati controllati dall'utente saranno collocati nella hash, si potrebbe anche voler:
security/security_csprng.cpp
security/security_csprng.h
invocarlo:
#include "templates/detachable_ordered_hash.h"
...
// The 47 is the nearest prime to a power of two
// that is close to your data size.
//
// If your brain hurts, just use the lookup table
// in 'detachable_ordered_hash.cpp'.
//
// If you don't care about some minimal memory thrashing,
// just use a value of 3. It'll auto-resize itself.
int y;
CubicleSoft::OrderedHash<int> TempHash(47);
// If you need a secure hash (many hashes are vulnerable
// to DoS attacks), pass in two randomly selected 64-bit
// integer keys. Construct with CSPRNG.
// CubicleSoft::OrderedHash<int> TempHash(47, Key1, Key2);
CubicleSoft::OrderedHashNode<int> *Node;
...
// Push() for string keys takes a pointer to the string,
// its length, and the value to store. The new node is
// pushed onto the end of the linked list and wherever it
// goes in the hash.
y = 80;
TempHash.Push("key1", 5, y++);
TempHash.Push("key22", 6, y++);
TempHash.Push("key3", 5, y++);
// Adding an integer key into the same hash just for kicks.
TempHash.Push(12345, y++);
...
// Finding a node and modifying its value.
Node = TempHash.Find("key1", 5);
Node->Value = y++;
...
Node = TempHash.FirstList();
while (Node != NULL)
{
if (Node->GetStrKey()) printf("%s => %d\n", Node->GetStrKey(), Node->Value);
else printf("%d => %d\n", (int)Node->GetIntKey(), Node->Value);
Node = Node->NextList();
}
Mi sono imbattuto in questo thread SO durante la mia fase di ricerca per vedere se qualcosa come OrderedHash esistesse già senza che mi fosse necessario inserire una libreria enorme. Io ero delusa. Così ho scritto il mio. E ora l'ho condiviso.
Buona parte del tempo di ricerca rapida per std :: map ha a che fare con il fatto che è ordinato in ordine, quindi può fare la ricerca binaria. Non puoi avere la tua torta e mangiarla anche tu! – bobobobo
Cosa hai finito usando allora? – aggsol