How to access map key and mapped types

Lets say, for example, I want to make a "get()" function for a map outside of the map class (for some reason). The implementation says that std::map has members map::key_type and map::mapped_type. However, I can't seem to access them.

1
2
3
4
5
6
7
8
std::map<std::string,std::string> tbl;

tbl.mapped_value get(tbl.key_value s) {
    if(tbl.find(s) == tbl.end()) {
        throw std::runtime_error{"key s does not exist."};
    }
    return tbl[s];
}


As a note, neither of...

1
2
tbl::mapped_value
tbl::key_value


...work either. How can I access that data?
Last edited on
TinyTertle wrote:
The implementation says that std::map has members map::key_type and map::mapped_type.


TinyTertle wrote:
tbl.mapped_value get(tbl.key_value s) {


As you said a few lines earlier, the types are named "key_type" and "value_type", not "key_value" or "mapped_value". And member types are accessed using the :: operator on the type:

auto get(decltype(tbl)::key_type s) {

Also, aren't you simply re-implementing map::at?
OP: what is the rationale to throw std::runtime_error if key doesn't exist?
Well that was pretty dumb of me. Also...
aren't you simply re-implementing map::at?


Lets say, for example, I want to make a "get()" function for a map outside of the map class (for some reason)
Last edited on
Topic archived. No new replies allowed.