如何将文本文件读入类的向量 [英] How to read in a text file into a vector of a class

查看:48
本文介绍了如何将文本文件读入类的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将给定文本文件中的行读入一类字符串的向量中.到目前为止,我已经掌握了这些,但是我无法将它们纳入向量.我试图创建一个while循环来读取用户输入的文本文件的每一行.我从上次修改过它,但是它所做的全部都一次读完,将拉斯维加斯和维加斯一分为二.我还需要将文本文件中的信息推送到我的Flight vector中.

I need to read in lines from a given text file into a vector of a class of strings. I have this so far but I cannot get them into the vector. I am trying to create a while loop to read in each line of the text file input by the user. I've modified it from the last time, but all it does is read in one thing at a time, splitting Las and Vegas. I also need to push the information from my text file into my Flight vector.

#include "sort.h"
#include "flight.h"
#include "std_lib_facilities_4.h"

vector<Flight> flights;

int main()
{
    Flight fly;
    string flightno, destination, departure, gateno;
    ifstream infile;
    string fileloc;
    cout << "Enter the file you would like to use:\n";
    getline(cin, fileloc);
    infile.open(fileloc);

    /*istringstream foo{"BA036 DALLAS 21:00 A3"};
    Flight bar;
    foo >> bar;
    bar.print();*/

    string line;
    getline(infile, line);
    while (infile >> line)
    {
        istringstream foo{line};
        foo >> fly;
        fly.print();
    }
    cout << endl;
}

两个h文件如下所示.我不知道它们是否可以接受文件行,因此,如果您有任何建议,将不胜感激.

The two h files look like this. I don't know if they are built to take in the file lines, so if you have any suggestions, that would be greatly appreciated.

#ifndef SORT_H_
#define SORT_H_
#include "flight.h"
using namespace std;

class Sort 
{
    protected:
        unsigned long num_cmps;                             // number of comparisons performed in sort function

    public:
        virtual void sort(vector<Flight>& data) = 0;        // main entry point
        bool testIfSorted(const vector<Flight>& data);      // returns false if not sorted
                                                            // true otherwise
        unsigned long getNumCmps() { return num_cmps; }     // returns # of comparisons
        void resetNumCmps() { num_cmps = 0; }
};

class SelectionSort:public Sort                             // SelectionSort class
{
    public:
        void sort(vector<Flight>& data);                    // main entry point
};

class BubbleSort:public Sort                                // BubbleSort class
{
    public:
        void sort(vector<Flight>& data);                    // main entry point
};


#endif //SORT_H_

#ifndef FLIGHT_H_
#define FLIGHT_H_
#include "std_lib_facilities_4.h"

#ifndef FLIGHT_H_
#define FLIGHT_H_
#include "std_lib_facilities_4.h"

class Flight
{
    string flightno, destination, departure, gateno;
    public:
        void print(){cout << flightno << ' ' << destination << ' ' << departure << ' ' << gateno << endl;}
        friend istream& operator>>(istream& is, Flight& flight);
};

istream& operator>>(istream& is, Flight& flight)
{
    is >> flight.flightno >> flight.destination >> flight.departure >> flight.gateno;
    return is;
}
#endif

这也是我们将要测试的文本文件的格式.

This is also the format of the text file that we will be testing.

FLIGHT NUMBER DESTINATION DEPARTURE TIME GATE NUMBER
AA223 LAS VEGAS 21:15 A3
BA036 DALLAS 21:00 A3
AA220 LONDON 20:30 B4
VI303 MEXICO 19:00 B4
BA087 LONDON 17:45 B4
AA342 PARIS 16:00 A7
VI309 PRAGUE 13:20 F2
QU607 TORONTO 8:30 F2
AA224 SYDNEY 8:20 A7
AF342 WASHINGTON 7:45 A3

很抱歉这么长的问题,我们将不胜感激.谢谢.

Sorry for such a long question, any help would be greatly appreciated. Thank you.

推荐答案

您应该添加 Flight :: operator>> 请注意, istream :: operator<< 在空白处中断.因此,如果要使用此代码,则需要从拉斯维加斯"中删除空格.

You should add an Flight::operator>> Note that the istream::operator<< breaks on whitespace. So if you want to use this code you'd need to take the space out of "LAS VEGAS".

例如:

#include <iostream>
#include <sstream>

using namespace std;

class Flight
{
    string flightno, destination, departure, gateno;
    public:
    void print(){cout << flightno << ' ' << destination << ' ' << departure << ' ' << gateno << endl;}
    friend std::istream& operator>>(std::istream& is, Flight& flight);
};

std::istream& operator>>(std::istream& is, Flight& flight)
{
    is >> flight.flightno >> flight.destination >> flight.departure >> flight.gateno;
    return is;
}

int main() {
    istringstream foo{"BA036 DALLAS 21:00 A3"};
    Flight bar;

    foo >> bar;
    bar.print();

    return 0;
}    

这篇关于如何将文本文件读入类的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆