Map with key是c++中多个值的组合
map with key which is combination of multiple values in c++
我想要一个c++中的映射,其中它的键是多个值的组合。我可以同时使用stl和boost
键值可以是字符串/整数,就像下面
typedef value_type int;
typedef key(string, template(string,int), template(string,int)) key_type;
typedef map(key_type, value_type) map_type;
map_type map_variable;
map_variable.insert(key_type("keyStrning1", 1, "keyString2"), 4);
map_variable.insert(key_type("keyStrning3", 1, "keyString2"), 5);
现在这个映射将包含两个条目,我将能够像下面这样找到它:
map_variable.find(key_type("keyStrning3", 1, "keyString2")).
我可以使用嵌套映射,但我想知道是否有使用boost或c++ stl的任何方便的解决方案。
您可以使用boost::variant
(或std::variant
当 c++ 17将准备好)。
#include <tuple>
#include <map>
#include <utility>
#include <boost/variant/variant.hpp>
typedef int ValueType;
typedef boost::variant<std::string, int> StrOrInt;
typedef std::tuple<std::string, StrOrInt, StrOrInt> KeyType;
typedef std::map<KeyType, ValueType> MapType;
int main(int argc, char *argv[]) {
MapType amap;
amap.insert(std::make_pair(
std::make_tuple("string1", "string2", 3), <--- key
4)); // <--- value
auto finder = amap.find(std::make_tuple("string1", "string2", 3));
std::cout << finder->second << 'n'; // <--- prints 4
return 0;
}
相关文章:
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 可组合的lambda/std::函数与std::可选
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- Problems with std::cin.fail()
- 混合组合和继承的C++问题
- 我需要将多个函数组合为一个函数
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 通过组合不同的类型来创建唯一的id
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 模板元编程:如何将参数包组合成新的参数包
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 检查向量是否具有所有可能的字符组合
- 如何在加密++中将两个源组合成新的源
- 根中的组合
- 这对"With a stackless coroutine, only the top-level routine may be suspended."意味着什么
- Boost.TEST with CLion: "Test framework quit unexpectedly"
- 避免碎片化的ClientHellos with OpenSSL (DTLS)
- Issues with Win32 ReadProcessMemory API
- 更改 C++ 中的组合分类变量
- Map with key是c++中多个值的组合