Search code examples
c++multimap

Declare a multimap with four values for a key


What syntax should I use to declare a multimap with four values for a key?

I would like to add two more values from the type unsigned int after the sc_core sc_time values.

      std::multimap<std::string, std::pair<sc_core::sc_time, sc_core::sc_time> > 

Thanks


Solution

  • You can use a tuple for that:

    std::tuple<key_type1, key_type2, key_typ3, key_typ4> 
    

    For example:

    #include <map>
    #include <string>
    #include <tuple>
    
    int main(int argc, char* argv[])
    {
        std::map<std::tuple<int, int, float, float>, std::string> myMap;  // if you meant 4 values as a key
        std::map<std::string, std::tuple<int, int, float, float>> myMap2; // if you meant 4 values for each string key
    
        return 0;
    }
    

    Also, I would like to note that when declaring a map, the template argument for the key goes first and then the value type (see here). Your post was ambiguously formulated so I didn't know if the four values were supposed to be the key or the value so I showed both possibilities.

    EDIT: As Jamin Grey nicely pointed out you can shorten this unfathomably long type using a typedef:

    typedef std::tuple<int, int, float, float> MyKeyType;
    

    Once you've done this you can use MyKeyType in your code instead.