读取 FILE 并将数据存储在 map<string, vector<string>> c++98

To read FILE and store data in map<string, vector<string>> c++98

我有文件 bird.lst,我需要读取文件内容并将数据存储在 map 中,这里的想法是将鸟名存储在字符串中,那些具有一些属性值的需要存储在向量中。请帮忙

最终 map 如下所示, 例如:

 parrot.sh   ---->  eat    yes
                    fly    yes

bird.lst

下方的文件内容
parrot.sh
eat    yes
fly    yes

pigeon.sh
eat    yes
fly    yes

duck.sh
eat   yes
fly   no

flammingo.sh
eat   yes
fly   yes

eagle.sh
eat    yes
flay   yes

您需要一个嵌套循环。

  • 外面一个读鸟的名字(地图的关键)
  • 里面读取小鸟的属性(向量的值)

这是我想出的:

#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>


typedef std::vector<std::string> attribute_vector;
typedef std::map<std::string,attribute_vector> bird_map;

int main()
{
    std::ifstream file("bird.lst");

    bird_map birds;
    std::string key;
    while(std::getline(file,key))
    {
        attribute_vector attributes;
        std::string value;
        while(std::getline(file,value))
        {
            // in case it has windows encoding with end-of-line = \r\n
            if (!value.empty() &&
                value[value.size()-1] == '\r')
            {
                value.erase(value.size() - 1);
            }

            // if we found the empty string
            if(value.empty())
            {
                break;
            }

            // save the value into the vector
            attributes.push_back(value);
        }
        // save the bird into the map
        birds[key] = attributes;
    }

    // now print the data we collected
    for(bird_map::iterator bird = birds.begin();
        bird != birds.end();
        bird++)
    {
        std::cout << bird->first << "\n";
        for(attribute_vector::iterator attribute = bird->second.begin();
            attribute != bird->second.end();
            attribute++)
        {
            std::cout << "    " << *attribute << "\n";
        }
        std::cout << "\n";
    }

    return 0;
}

https://onlinegdb.com/1TBobUxE2 尝试(它说 C++17 作为编译器类型,但在“额外编译器标志”下的配置中我传递 -std=c++98

如果要从 yes/no 值中拆分属性,则:

#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <utility>

typedef std::pair<std::string,std::string> attribute_pair;
typedef std::vector<attribute_pair> attribute_vector;
typedef std::map<std::string,attribute_vector> bird_map;

int main()
{
    std::ifstream file("bird.lst");

    bird_map birds;
    std::string key;
    while(std::getline(file,key))
    {
        attribute_vector attributes;
        std::string value;
        while(std::getline(file,value))
        {
            // in case it has windows encoding with end-of-line = \r\n
            if (!value.empty() &&
                value[value.size()-1] == '\r')
            {
                value.erase(value.size() - 1);
            }

            // if we found the empty string
            if(value.empty())
            {
                break;
            }

            // now split the value into an attribute and a flag
            attribute_pair attribute;
            std::istringstream ss(value);
            ss >> attribute.first >> attribute.second;

            // save the value into the vector
            attributes.push_back(attribute);
        }
        // save the bird into the map
        birds[key] = attributes;
    }

    // now print the data we collected
    for(bird_map::iterator bird = birds.begin();
        bird != birds.end();
        bird++)
    {
        std::cout << bird->first << "\n";
        for(attribute_vector::iterator attribute = bird->second.begin();
            attribute != bird->second.end();
            attribute++)
        {
            std::cout << "   " << attribute->first
                      << " = " << attribute->second
                      << "\n";
        }
        std::cout << "\n";
    }

    return 0;
}

https://onlinegdb.com/Htlh4eHu9 尝试(它说 C++17 作为编译器类型,但在“额外编译器标志”下的配置中我传递 -std=c++98

(Issue)问题不是很具体。 所以我会尝试做一个非常通用的解决方案。 定义函数:

bool is_file(string s) {
   return (s.substr((int)s.size() - 3, 3) == ".sh");
}

告诉你一个字符串是否是鸟文件。

由于(问题),我假设鸟的每个属性都是以下形式:(attribute, yes/no),我们将转换到(属性,1/0)。

现在,要阅读文件,您有多种选择。我会说出其中两个。

  1. 从控制台传递文件,即,如果您的 .exe 被调用 birds.exe,只需执行 birds.exe

  2. 使用 freopen("bird.lst", "r", stdin);然后用 std::cin.

    阅读

那么,主函数应该是这样的:

int main () {
   freopen("bird.lst", "r", stdin); // if you didnt read from console.
   map<string, vector<pair<string, bool>>> birds;
   string current_bird;
   while (cin >> s) {
       string s;
       cin >> s;
       if (is_file(s)) {
          current_bird = s;
          continue;
       }
       bool verdict;
       cin >> verdict;
       bird[current_bird].push_back(make_pair(s, verdict));
   }
}

并打印数据:

for (auto it = birds.begin(); it != birds.end(); it++) {
   cout << "Bird File: " << it.first << "\n";
   cout << "Attributes:\n";
   for (auto x : it.second) cout << x.first << " " << (x.second ? "YES" : "NO") << "\n";
}