#include#include int main() { // Create the hash table QHash hash; // Insert some values hash.insert(1, "one"); hash.insert(2, "two"); hash.insert(3, "three"); // Iterate over the hash table QHashIterator i(hash); while (i.hasNext()) { i.next(); qDebug() << i.key() << ":" << i.value(); } return 0; }
#includeThis example shows how to use QHash with a custom key type. It defines a Person class with a name and age, and overrides the qHash function to provide a hash function for Person objects. Then it creates a hash table of Person objects and string values and inserts some values. Finally, it looks up a value by key using the value function and prints it. The package library for QHash is Qt.#include class Person { public: Person(QString name, int age) : name_(name), age_(age) {} // Define the hash function for Person objects uint qHash(const Person& person) { return qHash(person.name_) ^ qHash(person.age_); } QString name_; int age_; }; int main() { // Create the hash table QHash hash; // Insert some values hash.insert(Person("Alice", 25), "Alice's value"); hash.insert(Person("Bob", 30), "Bob's value"); // Look up a value by key Person key("Alice", 25); qDebug() << hash.value(key); return 0; }