我对 STL 相当陌生。如果这个问题很幼稚,请原谅我。
我有一对这样的用作地图的钥匙。
typedef pair <int, int> KeyPair;
我的地图如下图
typedef map <KeyPair, uint32> NvInfoMap;
现在我想在地图的 Key 部分引入一个新整数。
哪种方法最简单?
我是否必须制作另一对将现有对作为其后半部分?
请注意,我所在的环境受限,boost 库不可用。
谢谢你的时间。
如果您的限制允许 C++11,那么
typedef std::tuple<int, int, int> KeyTriple;
Run Code Online (Sandbox Code Playgroud)
否则,您可以定义自己的类型
struct KeyTriple {
int a;
int b;
int c;
};
Run Code Online (Sandbox Code Playgroud)
有一个允许它被用作密钥的命令
bool operator<(KeyTriple const & lhs, KeyTriple const & rhs) {
if (lhs.a < rhs.a) return true;
if (rhs.a < lhs.a) return false;
if (lhs.b < rhs.b) return true;
if (rhs.b < lhs.b) return false;
if (lhs.c < rhs.c) return true;
return false;
// Alternatively, if you can use C++11 but don't want a tuple for a key
return std::tie(lhs.a, lhs.b, lhs.c) < std::tie(rhs.a, rhs.b, rhs.c);
}
Run Code Online (Sandbox Code Playgroud)
或者,正如您所建议的,您可以使用嵌套对
typedef std::pair<int, std::pair<int, int>>;
Run Code Online (Sandbox Code Playgroud)
优点是它为您定义了必要的比较运算符,但缺点是创建一个并访问其元素有点麻烦。
| 归档时间: |
|
| 查看次数: |
1602 次 |
| 最近记录: |