Reference as key in std::map

后端 未结 4 699
傲寒
傲寒 2021-02-07 12:39

Suppose some data structure:

typedef struct {
    std::string s;
    int i;
} data;

If I use the field data.s as key when adding i

4条回答
  •  情话喂你
    2021-02-07 13:06

    C++11

    Since C++11 reference wrapper is part of standard.

    #include  
    
    std::map, 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, data>
    

提交回复
热议问题