QMap类、 QHash类
QMap<QString, QString> infoMap;
infoMap.insert("张三", "23岁");
infoMap.insert("李四", "24岁");
infoMap["王五"] = "25岁";
infoMap["赵六"] = "26岁";
qDebug() << "张三: " <<infoMap["张三"];
qDebug() << "李四: " <<infoMap.value("李四");
qDebug() << "23岁的: " << infoMap.key("23岁");
QMapIterator<QString, QString> it_map_r(infoMap);
for(it_map_r.toFront(); it_map_r.hasNext();){
it_map_r.next();
qDebug() << it_map_r.key() << ": " << it_map_r.value();
}
QMutableMapIterator<QString, QString> it_map_rw(infoMap);
if(it_map_rw.findNext("23岁")){
it_map_rw.setValue("33岁");
}
for(it_map_rw.toFront(); it_map_rw.hasNext();){
it_map_rw.next();
qDebug() << it_map_rw.key() << ": " << it_map_rw.value();
}
QMap<int, QString> classMap;
classMap.insert(101, "张三老师");
classMap.insert(202, "李四老师");
classMap.insert(303, "王五老师");
classMap.insert(104, "赵六老师");
QMap<int, QString>::const_iterator it_class_r = classMap.constBegin();
for(; it_class_r != classMap.constEnd(); it_class_r++){
qDebug() << it_class_r.key() << ": " << it_class_r.value();
}
QMap<int, QString>::iterator it_class_rw = classMap.begin();
it_class_rw = classMap.find(101);
if(it_class_rw != classMap.end()){
it_class_rw.value() = "法外狂徒张三";
}
for(it_class_rw = classMap.begin(); it_class_rw != classMap.end(); it_class_rw++){
qDebug() << it_class_rw.key() << ": " << it_class_rw.value();
}
QMultiMap<int, QString> m_calssMap;
m_calssMap.insert(101, "张三老师");
m_calssMap.insert(101, "周八老师");
m_calssMap.insert(101, "李四老师");
m_calssMap.insert(303, "王五老师");
m_calssMap.insert(104, "赵六老师");
m_calssMap.insert(104, "钱九老师");
m_calssMap.remove(104, "钱九老师");
qDebug() << m_calssMap.values(101);
QMultiMap<int, QString>::iterator it_m_class_rw = m_calssMap.begin();
for(; it_m_class_rw != m_calssMap.end(); it_m_class_rw++){
qDebug() << it_m_class_rw.key() << ": " << it_m_class_rw.value();
}
?
QHash
QHash<int, QString> moneyHash;
moneyHash.insert(1, "壹元");
moneyHash.insert(5, "伍元");
moneyHash.insert(10, "拾元");
moneyHash[20] = "贰拾元";
moneyHash[50] = "伍拾元";
moneyHash[100] = "壹百元";
moneyHash.insertMulti(100, "壹佰元人民币");
moneyHash.remove(20);
QHashIterator<int, QString> it_money_r(moneyHash);
for(it_money_r.toFront(); it_money_r.hasNext();){
qDebug() << it_money_r.key() << ": " << it_money_r.next().value();
}
QMutableHashIterator<int, QString> it_money_rw(moneyHash);
if(it_money_rw.findNext("拾元")){
it_money_rw.setValue("10元");
}
for(it_money_rw.toFront(); it_money_rw.hasNext();){
qDebug() << it_money_rw.key() << ": " << it_money_rw.next().value();
}
QHash<int ,QString>::const_iterator it_money_const = moneyHash.constBegin();
for(; it_money_const != moneyHash.constEnd(); it_money_const++){
qDebug() << it_money_const.key() << ": "<<it_money_const.value();
}
QHash<int, QString>::iterator it_money = moneyHash.find(10);
if(it_money != moneyHash.end()){
it_money.value() = "拾元";
}
for(it_money = moneyHash.begin(); it_money != moneyHash.end(); it_money++){
qDebug() << it_money.key() << ": "<<it_money.value();
}
|