【发布时间】:2026-01-07 15:40:01
【问题描述】:
有这样一个任务:从 QMap 中按键删除项目。
我用这段代码来做这个。
QMap <int, QString> map;
map.insert(0, "0");
map.insert(1, "1");
map.insert(2, "2");
map.insert(3, "3");
map.insert(4, "4");
map.insert(5, "5");
qDebug() << "Before:";
for (auto i = 0; i < map.size(); i++)
qDebug() << map.value(i) << "\t";
qDebug() << "--------------";
map.remove(3);
qDebug() << "After:";
for (auto i = 0; i < map.size(); i++)
qDebug() << map.value(i) << "\t";
我有以下结果:
之前: “0” “1” “2” “3” “4” “5”
之后: “0” “1” “2” “” “4”
但我希望结果是:
之前: “0” “1” “2” “3” “4” “5”
之后:
“0” “1” “2” “4” “5”
请告诉我怎么了?
【问题讨论】:
-
试试map.erase(3)函数可能对你有帮助*.com/questions/799314/…