Mi piacerebbe vedere un semplice esempio di come sovrascrivere stdext :: hash_compare correttamente, al fine di definire una nuova funzione di hash e un operatore di confronto per il mio tipo definito dall'utente. Sto usando Visual C++ (2008).Come usare stdext :: hash_map?
7
A
risposta
1
Qui si va, ad esempio da MSDN
8
In questo modo si può fare
class MyClass_Hasher {
const size_t bucket_size = 10; // mean bucket size that the container should try not to exceed
const size_t min_buckets = (1 << 10); // minimum number of buckets, power of 2, >0
MyClass_Hasher() {
// should be default-constructible
}
size_t operator()(const MyClass &key) {
size_t hash_value;
// do fancy stuff here with hash_value
// to create the hash value. There's no specific
// requirement on the value.
return hash_value;
}
bool operator()(const MyClass &left, const MyClass &right) {
// this should implement a total ordering on MyClass, that is
// it should return true if "left" precedes "right" in the ordering
}
};
Poi, si può semplicemente utilizzare
stdext::hash_map my_map<MyClass, MyValue, MyClass_Hasher>
+0
Penso che l'operatore() s dovrebbe essere la funzione 'const' o MSVC2005 genererà l'errore c3849 – avee
0
Io preferisco usare una funzione non membro .
Il metodo utilizzato nell'articolo Extending boost::hash for a custom data type della documentazione Boost sembra funzionare.
Problemi correlati
- 1. Come usare stdext :: hash_map dove la chiave è un oggetto personalizzato?
- 2. Vale la pena usare std :: tr1 in produzione?
- 3. mappa vs hash_map in C++
- 4. Posso usare una variabile membro come chiave per un hash_set/hash_map?
- 5. Mi piacerebbe vedere un esempio di hash_map in C++
- 6. Come eliminare l'avviso di deprecazione di hash_map g ++?
- 7. Come usare unordered_map in Android?
- 8. Non riesci a trovare hash_map intestazione in Mac OSX
- 9. Come risolvere "<hash_map> è deprecato e sarà RIMOSSO. Utilizzare <unordered_map>"?
- 10. "hash_map" non è stato dichiarato in questo ambito con g ++ 4.2.1
- 11. Come usare TDictionary?
- 12. Come usare lo sprof?
- 13. Come usare Jquery contiene?
- 14. Come usare i simboli + =, * =, - =
- 15. Come usare cscope?
- 16. Come usare imageList Control
- 17. Come usare CSSStyleSheet.insertRule() correttamente?
- 18. come usare ansible b64encode
- 19. Come usare HostListener
- 20. Come usare scala-time?
- 21. Come usare MySQL DECIMAL?
- 22. come usare OpenSMPP
- 23. Come usare split?
- 24. Come usare bootstrap datepicker
- 25. Come usare templateRef?
- 26. come usare kafka acls?
- 27. Come usare MonadRandom?
- 28. Come usare ActionMenuView?
- 29. come usare next_permutation
- 30. Come usare spl_autoload_register?
Questo esempio non sovraccarica la funzione hash. – batty