简体   繁体   English

使用C ++中的向量在新行上显示文本文件中的列

[英]displaying columns from text file on new rows using vectors in c++

I've been trying to display 2 rows with 6 columns, on different rows. 我一直试图在不同的行上显示2行和6列。 I've tried different methods, none of which seems to be working. 我尝试了不同的方法,但似乎都没有用。 This is what I have for now: 这是我现在拥有的:

void Travel::LoadDest()
{

    ifstream file("worlddeals.txt");
    vector<vector<string>>data;

    if (file) {
        string line;
        while (getline(file, line)) {
            data.push_back(vector<string>());

            stringstream split(line);
            string value;

            while (split >> value)
                data.back().push_back(value);
        }
    }
    for (int i = 0; i < data.size(); i++) {
        for (int j = 0; j < data[i].size(); j++)
            cout << data[i][j] << endl;

        cout << '\n';
    }
}

In my .txt I have: 在我的.txt文件中,我有:

Corfu [Tab] 5* Gelina Village Waterpark [Tab] 21 May 2017 [Tab] Luton [Tab] 10 nights [Tab] All Inclusive Corfu [Tab] 5 * Gelina Village Waterpark [Tab] 2017年5月21日[Tab] Luton [Tab] 10晚[Tab]全包

Holguin [Tab] 5* Paradisus Rio De Oro [Tab] 22 May 2017 [Tab] Manchester [Tab] 10 nights [Tab] All Inclusive Holguin [Tab] 5 * Paradisus Rio De Oro [Tab] 2017年5月22日[Tab]曼彻斯特[Tab] 10晚[Tab]全包

What I want the output to be is: 我想要的输出是:

Corfu 科孚岛

Accommodation: 5* Gelina Village Waterpark 住宿:5 *格林纳村水上乐园

Departure Date: 21 May 2017 出发日期:2017年5月21日

Departure Airport: Luton 出发机场:卢顿

Duration: 10 nights 持续时间:10晚

Board Basis: All Inclusive 董事会基础:全包


Holguin 奥尔金

Accommodation: 5* Paradisus Rio De OroStar 住宿:5 * Paradisus Rio De OroStar

Departure Date: 22 May 2017 出发日期:2017年5月22日

Departure Airport: Manchester 出发机场:曼彻斯特

Duration: 10 nights 持续时间:10晚

Board Basis: All Inclusive 董事会基础:全包

You said it's not working out for you, but I just tried that code I linked using your input data, and it printed out pretty much what you want. 您说这对您没有用,但是我只是尝试使用输入数据链接的代码,它几乎打印出您想要的内容。 I didn't print the field names, but here is the line parsed according to your specifications: 我没有打印字段名称,但是这是根据您的规范分析的行:

#include <iostream>
#include <sstream>

void print_tab_delimited_fields(const std::string &input)
{
    std::istringstream ss(input);
    std::string token;
    while (std::getline(ss, token, '\t')) {
        std::cout << token << '\n';
    }
}

int main()
{
    std::string line1 = "Corfu\t5 * Gelina Village Waterpark\t21 May 2017\tLuton\t10 nights\tAll Inclusive";
    print_tab_delimited_fields(line1);

    std::string line2 = "Holguin\t5 * Paradisus Rio De Oro\t22 May 2017\tManchester\t10 nights\tAll Inclusive";
    print_tab_delimited_fields(line2);
}

output 产量

Corfu
5 * Gelina Village Waterpark
21 May 2017
Luton
10 nights
All Inclusive
Holguin
5 * Paradisus Rio De Oro
22 May 2017
Manchester
10 nights
All Inclusive

It would be a trivial change to push the results to a vector, or just print them along with the field labels, since the data file is already assumed to have those exact fields in that exact order. 将结果推到矢量上,或者仅将它们与字段标签一起打印,将是一个微不足道的更改,因为已经假定数据文件具有按照确切顺序排列的那些确切字段。

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

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