将 ruby​​ 哈希转换为 c++地图

发布于 2024-12-10 14:55:13 字数 360 浏览 0 评论 0原文

有没有办法将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫离伤花 2024-12-17 14:55:13

只需使用纯简格式的输入:

#include <unordered_map>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

std::ifstream infile("thefile.txt");
std::string line;

std::unordered_map<std::string, std::vector<int>> v;

while (std::getline(infile, line)
{
  std::string key, sep;
  int n;

  std::istringstream iss(line);

  if (!(iss >> key >> sep)) { /* error */ }
  if (sep != "=>")          { /* error */ }

  while (iss >> n) v[key].push_back(n);

  // maybe check if you've reached the end of the line and error otherwise
  // or maybe add the option to end a line at a comment character
}

Just use plain-Jane formatted input:

#include <unordered_map>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

std::ifstream infile("thefile.txt");
std::string line;

std::unordered_map<std::string, std::vector<int>> v;

while (std::getline(infile, line)
{
  std::string key, sep;
  int n;

  std::istringstream iss(line);

  if (!(iss >> key >> sep)) { /* error */ }
  if (sep != "=>")          { /* error */ }

  while (iss >> n) v[key].push_back(n);

  // maybe check if you've reached the end of the line and error otherwise
  // or maybe add the option to end a line at a comment character
}
痴意少年 2024-12-17 14:55:13

是的,这是可能的。一个简单的解决方案可能如下所示:

#include <fstream>
#include <iterator>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

int main() {
    std::ifstream input("your_file.txt");
    std::map<std::string,std::vector<unsigned int>> stringMap;
    std::string key, dummy; // dummy is for eating the "=>"
    while(input >> key >> dummy) {
        std::copy(std::istream_iterator<int>(input), 
                  std::istream_iterator<int>(),
                  std::back_inserter(stringMap[key]));
        input.clear();
    }
}

一些注意事项:

  • 如果尚不存在,stringMap[key]将在地图中创建一个新条目
  • std::istream_iterator将尝试从文件中读取整数,直到发生错误(例如无法转换为整数的字符),或到达流末尾
  • input.clear() 清除流中的所有错误 (这上面的 std::copy 将始终以错误结束)
  • 如果您的键可以解析为整数,或者它们包含空格,则此解决方案将无法按预期工作

如果存在这些限制对你来说太严格了,你可以看看 Boost.Spirit.Qi

Yes it is possible. A simple solution could look like this:

#include <fstream>
#include <iterator>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

int main() {
    std::ifstream input("your_file.txt");
    std::map<std::string,std::vector<unsigned int>> stringMap;
    std::string key, dummy; // dummy is for eating the "=>"
    while(input >> key >> dummy) {
        std::copy(std::istream_iterator<int>(input), 
                  std::istream_iterator<int>(),
                  std::back_inserter(stringMap[key]));
        input.clear();
    }
}

Some notes:

  • stringMap[key] will create a new entry in the map if none exists yet
  • std::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 reached
  • input.clear() clears any errors from the stream (the std::copy above will always end in an error)
  • this solution will not work as expected if your keys can be parsed as integers, or if they contain spaces

If these limitations are to strict for you, you could look into Boost.Spirit.Qi.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文