映射中的任何类型的值
Any type of value in map
我执行了以下代码:
#include <string>
#include <map>
#include <vector>
typedef std::map<std::string, std::map<std::string, std::map<std::string, std::string> > > SCHEMA;
int main() {
SCHEMA tables;
// Schema table
tables["table_name"]["row_id"]["field_name"] = "value";
}
我想修改typedef,使其具有一个"row_id"作为数字索引和任何类型的值(某种自动检测)。我应该做些什么来实现这样的目标?
tables["table1"][0]["field1"] = "value of any type";
数字索引很简单,只需将std::string
替换为int
即可。"任何类型的值"可能很难获得正确的C++,如果不小心,可能会导致问题。您可以使用void *
(坏)或union
(好),但更好的选择可能是一些变体实现,例如boost::variant,因此typedef
将读取,例如:
typedef std::map<std::string, std::map<int, std::map<std::string, boost::variant<int, std::string> > > > SCHEMA;
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- C++类型特征,以查看是否可以<uint32_t>对类型"K"的任何变量调用"static_cast(k)"
- C++ 将 CIN 值存储到任何类型的数组中
- 是否有任何建议来统一函数类型限定符并简化可恶的函数类型?
- 为什么 std::lerp 不适用于任何已实现所需操作的类型?
- 对任何类型的元素数组进行排序
- 任何种类的分数 在任何类型的订单中
- C++ - 声明指向返回任何类型并获取任意数量参数的函数的指针
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- 检查模板中 nullptr 的函数指针,了解任何类型的可调用对象
- 如何制作可以接受任何类型的参数的 std::函数和 lambda
- std::map 保存任何值类型
- 为自定义打印调试实现传递任何类型的变量
- MSVC 错误:4 个重载中的任何一个都无法转换所有参数类型
- 我的代码中是否有任何类型的错误,因为它没有给出正确的输出
- 如何在不给它任何类型(如整数)的情况下定义某物?
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 重载运算符:常量与非常量返回类型:任何性能差异
- 通过引用传递自定义类型-任何性能优势