如何将文本文件读入类的向量中

时间:2014-11-26 15:09:50

标签: c++ file class text vector

我需要从给定的文本文件中读取一行字符串的向量。到目前为止,我有这个,但我不能让他们进入矢量。我正在尝试创建一个while循环来读取用户输入的文本文件的每一行。我从最后一次修改它,但它所做的一切都是一次读取,分裂拉斯维加斯和拉斯维加斯。我还需要将文本文件中的信息推送到我的Flight向量中。

#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文件看起来像这样。我不知道它们是否构建为接收文件行,所以如果您有任何建议,我们将不胜感激。

#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

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

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

对于这么长的问题感到抱歉,非常感谢任何帮助。谢谢。

2 个答案:

答案 0 :(得分:3)

为了完全矫枉过正并训练我的现实生活中解决问题提升扒手:

一些更时髦的功能:

  • 它支持具有任意内容的目标,直到找到类似时间的列
  • 检查标题行
  • 它解释相对于当前当地时间的时间戳(如果出发时间比当前时间早30分钟以上,则假定为明天)

Live On Coliru

#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/phoenix/function.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/tag.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>

#include <boost/range/algorithm.hpp>

namespace qi = boost::spirit::qi;
namespace bmi = boost::multi_index;
namespace phx = boost::phoenix;

struct Flight {
    std::string id, destination, gate;
    boost::posix_time::ptime departure;

    friend std::ostream& operator<<(std::ostream& os, Flight const& f) {
        return os << "'" << f.id << "'\t'" << f.destination << "'\t" << f.departure << "\t'" << f.gate << "'";
    }
};

using Flights = bmi::multi_index_container<Flight,
      bmi::indexed_by<
        bmi::ordered_unique< bmi::tag<struct by_id>, bmi::member<Flight, std::string, &Flight::id> >,
        bmi::ordered_non_unique< bmi::tag<struct by_destination>, bmi::member<Flight, std::string, &Flight::destination> >,
        bmi::ordered_non_unique< bmi::tag<struct by_departure>, bmi::member<Flight, boost::posix_time::ptime, &Flight::departure> >,
        bmi::ordered_non_unique< bmi::tag<struct by_gate>, bmi::member<Flight, std::string, &Flight::gate> >
      >
  >;

/////////////////////////////////////////////////
// input parsing

BOOST_FUSION_ADAPT_STRUCT(Flight, (std::string,id)(std::string,destination)(boost::posix_time::ptime,departure)(std::string,gate))

boost::posix_time::ptime make_departure(int a_hours, int a_minutes) {
    auto today = boost::gregorian::day_clock::local_day();

    using namespace boost::posix_time;
    auto t_o_d     = second_clock::local_time().time_of_day();
    auto departure = hours(a_hours) + minutes(a_minutes);

    return departure < (t_o_d-minutes(30))
      ? ptime { today + boost::gregorian::days(1), departure }
      : ptime { today, departure };
}

BOOST_PHOENIX_ADAPT_FUNCTION(boost::posix_time::ptime, make_departure_, make_departure, 2)

using It = boost::spirit::istream_iterator;
static const qi::rule<It, std::string()> scol              = +qi::graph;
static const qi::uint_parser<unsigned, 10, 1, 2> time_part = {};
static const qi::rule<It, boost::posix_time::ptime()> dcol = (time_part > ':' > time_part) [ qi::_val = make_departure_(qi::_1, qi::_2) ];
static const qi::rule<It, std::string()> dest_col          = +(qi::graph >> *(+qi::space >> !dcol));

/////////////////////////////////////////////////
int main()
{
    std::cout << "Parsing with local date-time: " << boost::posix_time::second_clock::local_time() << "\n";

    Flights database;
    It first(std::cin >> std::noskipws), last;

    bool ok = qi::phrase_parse(first, last, qi::eps >> 
            "FLIGHT NUMBER" >> "DESTINATION" >> "DEPARTURE TIME" >> "GATE NUMBER" >> qi::eol
            >> (scol >> dest_col >> dcol >> scol) % qi::eol >> *qi::eol
            , qi::blank, database);

    if (ok) {
        std::cout << "Parse success\n";

        boost::copy(database.get<by_id>(),          std::ostream_iterator<Flight>(std::cout << "----- by flight number:\n", "\n"));
        boost::copy(database.get<by_destination>(), std::ostream_iterator<Flight>(std::cout << "----- by destination:\n", "\n"));
        boost::copy(database.get<by_departure>(),   std::ostream_iterator<Flight>(std::cout << "----- by departure time:\n", "\n"));
        boost::copy(database.get<by_gate>(),        std::ostream_iterator<Flight>(std::cout << "----- by gate:\n", "\n"));
    } else {
        std::cout << "Parse failed\n";
    }

    if (first != last)
        std::cout << "Remaining input: '" << std::string(first,last) << "'\n";
}

打印哪些:

Parsing with local date-time: 2014-Nov-28 17:58:46
Parse success
----- by flight number:
'AA220' 'LONDON'    2014-Nov-28 20:30:00    'B4'
'AA223' 'LAS VEGAS' 2014-Nov-28 21:15:00    'A3'
'AA224' 'SYDNEY'    2014-Nov-29 08:20:00    'A7'
'AA342' 'PARIS' 2014-Nov-29 16:00:00    'A7'
'AF342' 'WASHINGTON'    2014-Nov-29 07:45:00    'A3'
'BA036' 'DALLAS'    2014-Nov-28 21:00:00    'A3'
'BA087' 'LONDON'    2014-Nov-28 17:45:00    'B4'
'QU607' 'TORONTO'   2014-Nov-29 08:30:00    'F2'
'VI303' 'MEXICO'    2014-Nov-28 19:00:00    'B4'
'VI309' 'PRAGUE'    2014-Nov-29 13:20:00    'F2'
----- by destination:
'BA036' 'DALLAS'    2014-Nov-28 21:00:00    'A3'
'AA223' 'LAS VEGAS' 2014-Nov-28 21:15:00    'A3'
'AA220' 'LONDON'    2014-Nov-28 20:30:00    'B4'
'BA087' 'LONDON'    2014-Nov-28 17:45:00    'B4'
'VI303' 'MEXICO'    2014-Nov-28 19:00:00    'B4'
'AA342' 'PARIS' 2014-Nov-29 16:00:00    'A7'
'VI309' 'PRAGUE'    2014-Nov-29 13:20:00    'F2'
'AA224' 'SYDNEY'    2014-Nov-29 08:20:00    'A7'
'QU607' 'TORONTO'   2014-Nov-29 08:30:00    'F2'
'AF342' 'WASHINGTON'    2014-Nov-29 07:45:00    'A3'
----- by departure time:
'BA087' 'LONDON'    2014-Nov-28 17:45:00    'B4'
'VI303' 'MEXICO'    2014-Nov-28 19:00:00    'B4'
'AA220' 'LONDON'    2014-Nov-28 20:30:00    'B4'
'BA036' 'DALLAS'    2014-Nov-28 21:00:00    'A3'
'AA223' 'LAS VEGAS' 2014-Nov-28 21:15:00    'A3'
'AF342' 'WASHINGTON'    2014-Nov-29 07:45:00    'A3'
'AA224' 'SYDNEY'    2014-Nov-29 08:20:00    'A7'
'QU607' 'TORONTO'   2014-Nov-29 08:30:00    'F2'
'VI309' 'PRAGUE'    2014-Nov-29 13:20:00    'F2'
'AA342' 'PARIS' 2014-Nov-29 16:00:00    'A7'
----- by gate:
'AA223' 'LAS VEGAS' 2014-Nov-28 21:15:00    'A3'
'BA036' 'DALLAS'    2014-Nov-28 21:00:00    'A3'
'AF342' 'WASHINGTON'    2014-Nov-29 07:45:00    'A3'
'AA342' 'PARIS' 2014-Nov-29 16:00:00    'A7'
'AA224' 'SYDNEY'    2014-Nov-29 08:20:00    'A7'
'AA220' 'LONDON'    2014-Nov-28 20:30:00    'B4'
'VI303' 'MEXICO'    2014-Nov-28 19:00:00    'B4'
'BA087' 'LONDON'    2014-Nov-28 17:45:00    'B4'
'VI309' 'PRAGUE'    2014-Nov-29 13:20:00    'F2'
'QU607' 'TORONTO'   2014-Nov-29 08:30:00    'F2'

答案 1 :(得分:2)

您应添加Flight::operator>> 请注意istream::operator<<在空白处打破。因此,如果你想使用这个代码,你需要占用“拉斯维加斯”的空间。

例如:

#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;
}