简体   繁体   English

读取文件并将其存储在 unordered_map 中的问题

[英]Problem reading file and storing it in unordered_map

I'm trying to read a file and store it's content into an unordered_map but I've got a little problem.我正在尝试读取文件并将其内容存储到unordered_map中,但我遇到了一个小问题。 This is my unordered_map :这是我的unordered_map

std::unordered_map<std::string, std::vector<double>> _users;

And this is the content of the file that I'm trying to read:这是我要阅读的文件的内容:

Mike 4 NA 8 NA NA
Lena NA 8 4 NA 9

I want to store the content in _users in a way that the key is the name, and inside the vectors we have the numbers associated to the name.我想以键是名称的方式将内容存储在_users中,并且在向量中我们有与名称相关联的数字。 Moreover I want NA to be equal to 0. So I managed to do this:此外,我希望NA等于 0。所以我设法做到了:

while ( std::getline(file, line))
    {
        std::istringstream iss(line);
        std::string key;
        double value;
        iss >> key;
        dict[key] = std::vector<double>();

        while (iss >> value)
        {
            dict[key].push_back(value);
        }
    }

But since value is a double , when checking NA it just stops the while loop and I just get, for example with Mike: Mike 4 .但是由于valuedouble ,所以在检查NA时,它只会停止 while 循环,我就会得到,例如 Mike: Mike 4 How can I do in order to get it to read NA and put it as 0 inside the vector?我该怎么做才能让它读取NA并将其作为 0 放在向量中? Thank you for your help!谢谢您的帮助!

For your inner loop, you could do:对于您的内部循环,您可以执行以下操作:

    std::string stringval;
    while (iss >> stringval)
    {
        double value;
        try
        {
            value = std::stod (stringval);
        }
        catch (...)
        {
            value = 0.0;
        }
        dict[key].push_back(value);
    }

I also tried something:我也尝试了一些东西:

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

int main(int argc, char*[])
{
        //1. Read the file and load the map
        std::unordered_map<std::string, std::vector<double>> users{};

        std::ifstream file{"file.txt"};
        std::string line{};

        while(std::getline(file, line))
        {
                std::istringstream iss(line);
                std::string key{};
                iss >> key;

                std::vector<double> values{};

                while(iss)
                {
                        double value{0};
                        if (iss.str() != "NA")
                                iss >> value;
                        values.push_back(value);
                }

                users.insert({key, values});
        }

        //2. Printing the map
        for(auto const &[key, values]: users)
        {
                std::cout << "Key: " << key << std::endl;
                for(auto const value: values)
                {
                        std::cout << value << " ";
                }
                std::cout << std::endl;
        }

        return EXIT_SUCCESS;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM