QHash<QString, int> hash;for(int i = 0; i < 50; ++i) hash.insert(QString::number(i), i);qDebug() << hash;
QHash<QString, QString> hash;hash.insert("s2", "2");hash.insert("s1", "1");QHashIterator<QString, QString> hi(hash);while (hi.hasNext()){hi.next();qDebug() << hi.key();}
C++ (Qt)#include <QHash>#include <QDebug>#include <math.h> int main( int argc, char ** argv ){ QHash<QString,QString> hash; qDebug() << "Generating..."; for ( int i = 0; i < 10; ++i ) { int r = rand(); QString key = QString( "Key: %1" ).arg( r ); QString value = QString( "Value: %1" ).arg( r ); hash[ key ] = value; qDebug() << "key:" << key << " value:" << value; } qDebug() << "Iterating..."; for ( QHashIterator<QString,QString> it( hash ); it.hasNext(); ) { it.next(); QString key = it.key(); QString value = it.value(); qDebug() << "key:" << key << " value:" << value; } return 0;}
Generating...key: "Key: 1804289383" value: "Value: 1804289383"key: "Key: 846930886" value: "Value: 846930886"key: "Key: 1681692777" value: "Value: 1681692777"key: "Key: 1714636915" value: "Value: 1714636915"key: "Key: 1957747793" value: "Value: 1957747793"key: "Key: 424238335" value: "Value: 424238335"key: "Key: 719885386" value: "Value: 719885386"key: "Key: 1649760492" value: "Value: 1649760492"key: "Key: 596516649" value: "Value: 596516649"key: "Key: 1189641421" value: "Value: 1189641421"Iterating...key: "Key: 1957747793" value: "Value: 1957747793"key: "Key: 1681692777" value: "Value: 1681692777"key: "Key: 1189641421" value: "Value: 1189641421"key: "Key: 1804289383" value: "Value: 1804289383"key: "Key: 1649760492" value: "Value: 1649760492"key: "Key: 596516649" value: "Value: 596516649"key: "Key: 424238335" value: "Value: 424238335"key: "Key: 846930886" value: "Value: 846930886"key: "Key: 1714636915" value: "Value: 1714636915"key: "Key: 719885386" value: "Value: 719885386"