将 ruby 哈希转换为 c++地图
有没有办法将 ruby 生成的哈希转换为 C++ 映射?我尝试将哈希打印到文件中,但不知道如何将其读入 C++ 映射中。
哈希值按以下方式打印:
stringA => 123 234 345 456 567
stringB => 12 54 103 313 567 2340
...
每个关联字符串的数字数量各不相同,并且字符串是唯一的。我想使用:
std::map<std::string,std::vector<unsigned int>> stringMap;
如何分别读取每行的字符串和数组部分?
Is there a way to convert a hash made in ruby to a C++ map? I've tried printing the hash into a file, but am unaware of how to read it into a C++ map.
The hash is printed in the following way:
stringA => 123 234 345 456 567
stringB => 12 54 103 313 567 2340
...
The amount of numbers varies for each associated string, and the strings are unique. I would like to use:
std::map<std::string,std::vector<unsigned int>> stringMap;
How can I read the string and array parts of each line separately?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用纯简格式的输入:
Just use plain-Jane formatted input:
是的,这是可能的。一个简单的解决方案可能如下所示:
一些注意事项:
stringMap[key]
将在地图中创建一个新条目std::istream_iterator
将尝试从文件中读取整数,直到发生错误(例如无法转换为整数的字符),或到达流末尾input.clear()
清除流中的所有错误 (这上面的std::copy
将始终以错误结束)如果存在这些限制对你来说太严格了,你可以看看 Boost.Spirit.Qi。
Yes it is possible. A simple solution could look like this:
Some notes:
stringMap[key]
will create a new entry in the map if none exists yetstd::istream_iterator<int>
will try to read integers from the file until an error happens (such as a character that cannot be converted to integer), or the end of the stream is reachedinput.clear()
clears any errors from the stream (thestd::copy
above will always end in an error)If these limitations are to strict for you, you could look into Boost.Spirit.Qi.