可以使用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