2

我应该使用什么语法来声明一个具有四个键值的多重映射?

我想在 sc_core sc_time 值之后再添加两个unsigned int类型的值。

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

谢谢

4

1 回答 1

5

您可以为此使用元组:

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

例如:

#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;
}

另外,我想指出,在声明映射时,键的模板参数首先出现,然后是值类型(参见此处)。你的帖子的表述含糊不清,所以我不知道这四个值应该是关键还是价值,所以我展示了这两种可能性。

编辑:正如 Jamin Gray 很好地指出的那样,您可以使用 typedef 缩短这个深不可测的长类型:

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

完成此操作后,您可以MyKeyType在代码中使用。

于 2013-07-07T02:14:41.103 回答