QMap属于关联式容器,其底层结构是通过二叉树实现,故其查找value的效率很快。QMap中的数据都是成对出现的,第一个称为key(键),第二个称value(键值)。
//定义
QMap<QString, int> map;
//插值
map.insert("twelve", 12);
//取值
int num2 = map.value("twelve");
//删除
map.remove("twelve");
//判斷是否存在某个值
int timeout = 30;
if (map.contains("twelve"))
timeout = map.value("twelve");
//遍历
QMap<QString, int>::const_iterator iter = map.constBegin();
while (iter != map.constEnd()) {
cout << iter.key() << ": " << iter.value() << endl;
++iter;
}
//遍历值
foreach (int value, map)
cout << value << endl;