void Admin::searchFlight(string destination)
{
Flight sFlight;
string read;
ifstream inFlight("Flight.txt");
while(getline(inFlight, read))
{
flightList.push_back(sFlight);
}
int counter = 0;
for(int i = 0; i < flightList.size(); i++)
{
if(destination.compare(flightList[i].getDestination()))
{
//counter++;
flightList[i].displayFlightSchedule();
}
}
}
文本文件中有一些数据,如何读取矢量中的文本文件?
这是我的重载构造函数Flight()
:
Flight flightSchedule(flightID,departure,destination,price,dateOfFlight,timeOfFlight);
这是我的矢量声明:
vector<Flight> flightList;
答案 0 :(得分:0)
根据您的要求,假设您的Flight.txt内容为:
印度22
USA 23
中国24
#include<iostream>
#include<vector>
#include<fstream>
#include<sstream>
using namespace std;
void searchFlight(string);
class Flight
{
public:
string destination;
int tm;
Flight(string d, int t):destination(d), tm(t)
{
}
string getDestination()
{
return destination;
}
int displayFlightSchedule()
{
return tm;
}
};
int main()
{
searchFlight("India");
return 0;
}
void searchFlight(string destin)
{
vector<Flight> flightlist;
string line;
int tm;
string dest;
ifstream ifs("Flight.txt");
while(getline(ifs, line))
{
istringstream is(line);
is>>dest>>tm;
flightlist.push_back(Flight(dest, tm));
}
for(unsigned i = 0; i<flightlist.size(); i++)
{
if (destin == (flightlist[i].getDestination()))
{
cout<<flightlist[i].displayFlightSchedule()<<endl;
}
}
}
<强>输出:强>
22