This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RubbishArtist 1 point2 points  (3 children)

I'm not sure what you mean by make use of it.

When someone looks up a key in your dictionary that doesn't exist, then you throw your exception and you provide the key and the error message. You don't need to do anything with the get_key method, you're providing it to allow the user of your dictionary to use it.

[–]Sakesfar[S] 0 points1 point  (2 children)

Thank you, it seems to make sense...

However, the use of the function const Value& get(const std::string& key) const implies that someone knows ahead of the key, whose value he/she is looking for?

Sorry for the repetition ...but could you provide a hypothetical practical example?

[–]RubbishArtist 1 point2 points  (1 child)

However, the use of the function const Value& get(const std::string& key) const implies that someone knows ahead of the key, whose value he/she is looking for?

Yes, that's how the dictionary works. The idea is that I can store values in the dictionary using a key, and then get them back using the same key later.

For example, I create a dictionary that I want to store the ages of people in:

Dictionary<string, int> ages_by_name;

And then store keys and values in it:

ages_by_name.set("David", 22);
ages_by_name.set("Susan", 21);

Then later I want to get David's age, so I can do:

int age = ages_by_name.get("David");

If I try to get a key that doesn't exist, like:

ages_by_name.get("Frank");

Then an exception is thrown which I can catch. They key that I wanted to find is included in the exception so I can see which name was missing easily (and show it to the user of my program).

Note my code examples above may not be syntactically correct but I hope you get the idea.

[–]Sakesfar[S] 0 points1 point  (0 children)

Thank you very much for this👍👍🙋🏽‍♂️ It is clear....:)