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



我有文件bird.list,我需要读取文件内容并将数据存储在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 = rn
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(

如果你想从是/否值中分离属性,那么:

#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 = rn
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(

(问题(问题不是很具体。因此,我将尝试制定一个非常通用的解决方案。定义一个函数:

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

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

由于(Issue(,我将假设鸟的每个属性的形式为:(attribute,yes/no(,我们将转换为(attribut,1/0(。

现在,要读取该文件,您有几个选项。我会说出其中两个的名字。

  1. 从控制台传递文件,即,如果你的.exe名为birds.exe,只需执行birds.exe<bird.lst。只需用std::cin阅读即可。

  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";
}

最新更新