Start learning free

LESSON 124 OF 667 · DICTIONARIES

Dictionary Length in C++

Lesson

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.

STEP 1 OF 1
    Ctrl + Enter to check