[英]Storing data in map<string, vector<std::pair<string, string>>> c++98
I have file named Bird.lst
.我有一个名为Bird.lst
文件。 I am trying to read its contents and store the data in a map<string, vector<pair<string, string>>>
.我正在尝试读取其内容并将数据存储在map<string, vector<pair<string, string>>>
。 The idea is to store the bird's name in the string
and its attribute values in the vector
.这个想法是将鸟的名字存储在string
并将其属性值存储在vector
。
Also, there are some attributes that I don't need ( vaccinated
, babies
, sale
).此外,还有一些我不需要的属性(已vaccinated
、 babies
、 sale
)。 While inserting into the map
, I have to check to not insert these attributes.在插入map
,我必须检查不插入这些属性。
Where the attributes didn't got added, but while displaying, the map
contents there are displaying empty spaces.没有添加属性的地方,但在显示时,那里的map
内容显示空白。 I want to get rid of the empty lines from the map
.我想摆脱map
中的空行。
My map
content should be seen as below.我的map
内容应该如下所示。 please help.请帮忙。
parrot.sh ----> eat yes
fly yes
Bird.lst鸟.lst
parrot.sh
vaccinated yes
eat yes
babies no
fly yes
sale no
pigeon.sh
vaccinated yes
eat yes
fly yes
babies yes
sale yes
duck.sh
vaccinated yes
eat yes
fly no
sale yes
babies no
flammingo.sh
vaccinated yes
eat yes
fly yes
sale no
babies no
eagle.sh
vaccinated yes
eat yes
babies no
fly yes
Code:代码:
#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);
if(value.find("vaccinated") == std::string::npos && value.find("babies") == std::string::npos && value.find("sale") == std::string::npos)
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;
}
Output from the above code:上面代码的输出:
duck.sh
eat yes
fly no
eagle.sh
eat yes
fly yes
flammingo.sh
eat yes
fly yes
parrot.sh
eat yes
fly yes
pigeon.sh
eat yes
fly yes
When you are parsing a value
string into an attribute_pair
, you are looking for specific attributes to ignore, and if you find one then you don't parse the value
, but you are still inserting the empty attribute_pair
into the attributes
vector.当您将一个value
字符串解析为一个attribute_pair
,您正在寻找要忽略的特定属性,如果您找到一个,则您不解析value
,但您仍然将空的attribute_pair
插入到attributes
向量中。 That is where the empty lines in your output are coming from.这就是输出中空行的来源。
Change this:改变这个:
// now split the value into an attribute and a flag
attribute_pair attribute;
std::istringstream ss(value);
if(value.find("vaccinated") == std::string::npos && value.find("babies") == std::string::npos && value.find("sale") == std::string::npos)
ss >> attribute.first >> attribute.second;
// save the value into the vector
attributes.push_back(attribute);
To this instead:对此:
// now split the value into an attribute and a flag
attribute_pair attribute;
std::istringstream ss(value);
ss >> attribute.first >> attribute.second;
if(attribute.first != "vaccinated" && attribute.first != "babies" && attribute.first != "sale") {
// save the value into the vector
attributes.push_back(attribute);
}
That being said, you might consider using a map<string,string>
instead of a vector<pair<string,string>>
, unless you have a requirement to preserve the original order of the attributes of each bird (you are not preserving the order of the birds themselves, since std::map
is sorted).话虽如此,您可能会考虑使用map<string,string>
而不是vector<pair<string,string>>
,除非您需要保留每只鸟的属性的原始顺序(您没有保留鸟类本身的顺序,因为std::map
已排序)。
Before inserting into map<key, value> here value is vector<pair<string, string>> > add addition check if value vector string is not empty.在插入 map<key, value> 之前,这里的 value 是 vector<pair<string, string>> > 添加附加检查 value vector string 是否为空。
#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);
if(value.find("vaccinated") == std::string::npos && value.find("babies") == std::string::npos && value.find("sale") == std::string::npos)
ss >> attribute.first >> attribute.second;
if(!attribute.first.empty()) //check the attribute value is empty or not before inserting into attributes vector
// 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.