c++

C++怎么统计map中key的数量

小亿
151
2024-01-11 01:42:08
栏目: 编程语言

可以使用map的size()函数来统计map中key的数量。size()函数返回的是map中键值对的数量,即key的数量。以下是一个示例代码:

#include <iostream>
#include <map>

int main() {
    std::map<int, std::string> myMap;
    myMap[1] = "Apple";
    myMap[2] = "Banana";
    myMap[3] = "Orange";

    int keyCount = myMap.size();
    std::cout << "The number of keys in the map is: " << keyCount << std::endl;

    return 0;
}

输出结果为:

The number of keys in the map is: 3

0
看了该问题的人还看了