Start learning free

LESSON 123 OF 667 · DICTIONARIES

Updating Dictionary Values in C++

Lesson

In C++, you update a value in an unordered_map by assigning a new value to an existing key:

unordered_map<string, int> scores;
scores["Alice"] = 95;
scores["Bob"] = 87;

scores["Alice"] = 99;     // update: Alice is now 99
scores["Charlie"] = 72;   // add new entry

The bracket operator [] both accesses and creates/updates entries. You can also use insert_or_assign() (C++17):

scores.insert_or_assign("Alice", 99);  // updates Alice

Assignment
Follow the Coding Tutorial and update Alice's score to 99!


Hint
Use scores["Alice"] = 99.

STEP 1 OF 1
    Ctrl + Enter to check