MaybeView

Note: this is based on the version of Ox in following revision in the Nostalgia repo : 1b629da8fc658a85f07b5209f2791a5ebdf79fa1 Problem In C++, hash maps are often used with strings as keys. This would typically look something like this. std::unordered_map<std::string, int> ages; ages["Jerry Smith"] = 54; And here is an example of a lookup: int age = ages["Jerry Smith"]; There is a hidden inefficiency here. The lookup operator does not take a std::string_view or a C string. The lookup operator takes a std::string. That means, even though we are passing in a C string that has all the necessary data, we are implicitly calling the std::string constructor, which will allocate space on the heap for the string data, then copy the existing C string into the buffer it allocated. Then, as soon as the lookup call is finished, the temporary std::string is destroyed. ...

April 24, 2024 · 2 min · Gary Talent