qmap判断是否存在某个key

 

#include <QMap>#include <QString>#include <iostream>int main() {
    QMap<QString, int> map;
    map["apple"] = 1;
    map["banana"] = 2;
    map["orange"] = 3;
    QString keyToCheck = "banana";
    int value = map.value(keyToCheck);
    if (value != 0) {
        std::cout << "Value for key " << keyToCheck << " is: " << value << std::endl;
    } else {
        std::cout << "Key " << keyToCheck << " does not exist in the map." << std::endl;
    }
    return 0;
}

 

 

========

posted @ 2024-01-07 21:38  西北逍遥  阅读(385)  评论(0编辑  收藏  举报