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