简体   繁体   English

将向量拆分为向量

[英]Split Vector into Vector of Vectors

I am currently working with a text file of results from Yelp containing restaurants and some other information. 目前,我正在处理Yelp的结果文本文件,其中包含餐馆和其他一些信息。 Each element for a restaurant is separated by a vertical line (|) and each restaurant is separated by a new line (\\n). 饭店的每个元素都由竖线(|)分隔,而饭店的每个元素都由新线(\\ n)分隔。 (See example below). (请参见下面的示例)。 I am trying to split this line into a vector of vectors. 我正在尝试将这条线分成多个向量。 The larger vector would hold all smaller vectors, while the smaller vectors would each hold the information for one of the restaurants. 较大的向量将保留所有较小的向量,而较小的向量将各自保留餐馆之一的信息。 How do I best approach this? 我如何最好地解决这个问题?

Here are a couple lines from the file: 这是文件中的几行:

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

Try something like this: 尝试这样的事情:

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...

With that said, assuming the meaning and order of the fields of each restaurant are always the same, you could alternatively define a struct to hold the individual fields and then create a vector of struct values instead, eg: 话虽如此,假设每个餐厅的字段的含义和顺序始终相同,则可以选择定义一个struct来保存各个字段,然后创建一个structvector ,例如:

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...

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

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