一、
二、
1.基本使用
QMap<QString,int> map_data_; void MainWindow::on_pushButton_clicked() { map_data_.insert("a",1); map_data_.insert("b",2); map_data_.insert("c",3); map_data_.insert("a",11);//替换掉以前的值 map_data_.remove("c");//移除数据,如果不存在,正常执行,不会异常 QMap<QString,int>::iterator iter; for(iter = map_data_.begin();iter != map_data_.end();iter++) { qDebug()<<iter.key()<<" "<<iter.value(); } }
2.判断key是否存在
//无论是QMap还是std::map判断key是否存在都可以通过count和find方法 //count:有返回1,无返回0 //find:有返回对应的迭代器,无返回尾后迭代器end() if(map_data_.count("a")>0) { qDebug()<<"the key exist!"; } else { qDebug()<<"the key doesn't exist!"; } if (map_data_.find("a") != map_data_.end()) { qDebug()<<"find,the key exist!"; } else { qDebug()<<"find,the key doesn't exist!"; }