将向量拆分为向量的向量
Split Vector into Vector of Vectors
我目前正在处理 Yelp 结果的文本文件,其中包含餐馆和一些其他信息。餐厅的每个元素由垂直线 (|) 分隔,每个餐厅由换行符 (\n) 分隔。 (见下面的例子)。我试图将这条线拆分成一个向量向量。较大的向量将包含所有较小的向量,而较小的向量将分别包含其中一家餐馆的信息。我该如何最好地解决这个问题?
以下是文件中的几行:
Meka's Lounge|42.74|-73.69|407 River Street+Troy, NY 12180|http ://www.yelp.com/biz/mekas-lounge-troy|Bars|5|2|4|4|3|4|5
Tosca Grille|42.73|-73.69|200 Broadway+Troy, NY 12180|http ://www.yelp.com/biz/tosca-grille-troy|American (New)|1|3|2|4
Happy Lunch|42.75|-73.68|827 River St+Troy, NY 12180|http ://www.yelp.com/biz/happy-lunch-troy|American (Traditional)|5|2
Hoosick Street Discount Beverage Center|42.74|-73.67|2200 19th St+Troy, NY 12180|http ://www.yelp.com/biz/hoosick-street-discount-beverage-center-troy|Beer, Wine & Spirits|4|5|5|5|5|4
尝试这样的事情:
std::vector< std::vector<std::string> > vecRestaurants;
std::ifstream in("restaurants.txt");
std::string line;
while (std::getline(in, line))
{
std::vector<std::string> info;
std::istringstream iss(line);
while (std::getline(iss, line, '|'))
info.push_back(line);
vecRestaurants.push_back(info);
}
in.close();
// use vecRestaurants as needed...
话虽如此,假设每家餐厅的字段的含义和顺序始终相同,您也可以定义一个 struct
来保存各个字段,然后创建一个 vector
struct
值代替,例如:
struct sRestaurantInfo
{
std::string name;
float field2; // ie 42.74, what is this?
float field3; // ie -73.69, what is this?
std::string address;
std::string url;
std::string type;
// what are the remaining numbers?
};
std::vector<sRestaurantInfo> vecRestaurants;
std::ifstream in("restaurants.txt");
std::string line;
while (std::getline(in, line))
{
sRestaurantInfo info;
std::istringstream iss(line);
std::getline(iss, info.name, '|');
std::getline(iss, line, '|'); std::istringstream(line) >> info.field2;
std::getline(iss, line, '|'); std::istringstream(line) >> info.field3;
std::getline(iss, info.address, '|');
std::getline(iss, info.url, '|');
std::getline(iss, info.type, '|');
// read the remaining numbers if needed...
vecRestaurants.push_back(info);
}
in.close();
// use vecRestaurants as needed...
我目前正在处理 Yelp 结果的文本文件,其中包含餐馆和一些其他信息。餐厅的每个元素由垂直线 (|) 分隔,每个餐厅由换行符 (\n) 分隔。 (见下面的例子)。我试图将这条线拆分成一个向量向量。较大的向量将包含所有较小的向量,而较小的向量将分别包含其中一家餐馆的信息。我该如何最好地解决这个问题?
以下是文件中的几行:
Meka's Lounge|42.74|-73.69|407 River Street+Troy, NY 12180|http ://www.yelp.com/biz/mekas-lounge-troy|Bars|5|2|4|4|3|4|5
Tosca Grille|42.73|-73.69|200 Broadway+Troy, NY 12180|http ://www.yelp.com/biz/tosca-grille-troy|American (New)|1|3|2|4
Happy Lunch|42.75|-73.68|827 River St+Troy, NY 12180|http ://www.yelp.com/biz/happy-lunch-troy|American (Traditional)|5|2
Hoosick Street Discount Beverage Center|42.74|-73.67|2200 19th St+Troy, NY 12180|http ://www.yelp.com/biz/hoosick-street-discount-beverage-center-troy|Beer, Wine & Spirits|4|5|5|5|5|4
尝试这样的事情:
std::vector< std::vector<std::string> > vecRestaurants;
std::ifstream in("restaurants.txt");
std::string line;
while (std::getline(in, line))
{
std::vector<std::string> info;
std::istringstream iss(line);
while (std::getline(iss, line, '|'))
info.push_back(line);
vecRestaurants.push_back(info);
}
in.close();
// use vecRestaurants as needed...
话虽如此,假设每家餐厅的字段的含义和顺序始终相同,您也可以定义一个 struct
来保存各个字段,然后创建一个 vector
struct
值代替,例如:
struct sRestaurantInfo
{
std::string name;
float field2; // ie 42.74, what is this?
float field3; // ie -73.69, what is this?
std::string address;
std::string url;
std::string type;
// what are the remaining numbers?
};
std::vector<sRestaurantInfo> vecRestaurants;
std::ifstream in("restaurants.txt");
std::string line;
while (std::getline(in, line))
{
sRestaurantInfo info;
std::istringstream iss(line);
std::getline(iss, info.name, '|');
std::getline(iss, line, '|'); std::istringstream(line) >> info.field2;
std::getline(iss, line, '|'); std::istringstream(line) >> info.field3;
std::getline(iss, info.address, '|');
std::getline(iss, info.url, '|');
std::getline(iss, info.type, '|');
// read the remaining numbers if needed...
vecRestaurants.push_back(info);
}
in.close();
// use vecRestaurants as needed...