Reference as key in std::map
C++11
Since C++11 reference wrapper is part of standard.
#include <functional>
std::map<std::reference_wrapper<std::string>, data>
Using Boost
You may want to take a look at boost.ref. It provides a wrapper that enables references to be used in STL-containers like this:
std::map<boost::reference_wrapper<std::string>, data>
You can't store references in Standard Library containers - your map should look like:
map <string,data> mymap;
The map will manage both the key string and the struct instances, which will be copies, for you. Both map
and unordered_map
work in the same way in this regard, as do all other Standard Library containers.
Note that in C++, you don't need typedefs to declare structs:
struct data {
std::string s;
int i;
};