In C++, you can get the number of key-value pairs in an unordered_map using the size() method:
unordered_map<string, int> scores;
scores["Alice"] = 95;
scores["Bob"] = 87;
scores["Charlie"] = 72;
int n = scores.size();
cout << n; // 3
size() returns the number of entries. You can also check if it's empty with empty():
cout << scores.empty(); // 0 (false)
unordered_map<string, int> empty;
cout << empty.size(); // 0
Assignment
Follow the Coding Tutorial and get the number of entries in scores!
Hint
Use scores.size() and assign it to count.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
