String格式map还原成map

public static Object getValue(String param) {

Map map = new HashMap();
String str = "";
String key = "";
Object value = "";
char[] charList = param.toCharArray();
boolean valueBegin = false;
for (int i = 0; i < charList.length; i++) {
char c = charList[i];
if (c == '{') {
if (valueBegin == true) {
value = getValue(param.substring(i, param.length()));
i = param.indexOf('}', i) + 1;
map.put(key, value);
}
} else if (c == '=') {
valueBegin = true;
key = str;
str = "";
} else if (c == ',') {
valueBegin = false;
value = str;
str = "";
map.put(key, value);
} else if (c == '}') {
if (str != "") {
value = str;
}
map.put(key, value);
return map;
} else if (c != ' ') {
str += c;
}
}
return map;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以的,你可以使用STL库中的std::map来替换QMap。下面是一个使用std::map来存储并以二进制格式写入文件的示例: ```c++ #include <map> #include <fstream> int main() { std::map<std::string, int> map; map.insert({"apple", 1}); map.insert({"banana", 2}); map.insert({"orange", 3}); std::ofstream file("data.bin", std::ios::binary); if (file.is_open()) { file.write(reinterpret_cast<const char*>(&map), sizeof(map)); file.close(); } return 0; } ``` 在上面的代码中,我们创建了一个std::map对象,并向其中插入了一些数据。然后我们创建了一个std::ofstream对象,并以二进制文件模式打开它。接着,我们将map对象的地址强制转换为const char*类型,并使用std::ofstream的write函数将其写入到文件中。 如果需要读取该文件中的Map对象,可以使用std::ifstream的read函数来读取。需要注意的是,在读取Map对象之前,必须先将std::map的实例化对象map定义出来。以下是读取二进制文件并还原Map对象的示例代码: ```c++ #include <map> #include <fstream> int main() { std::map<std::string, int> map; std::ifstream file("data.bin", std::ios::binary); if (file.is_open()) { file.read(reinterpret_cast<char*>(&map), sizeof(map)); file.close(); } // 输出Map对象中的元素 for (const auto& item : map) { std::cout << item.first << ": " << item.second << std::endl; } return 0; } ``` 在上面的代码中,我们先创建一个空的std::map对象。然后我们打开之前写入的二进制文件,并使用std::ifstream的read函数读取Map对象。最后,我们遍历Map对象中的元素,并输出它们的键和值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值