Some observations that I hope helpful (but be careful: the following code is untested).
If your Passenger
need first name, last name and destination, I suppose that there is a Passenger
constructor with three string parameters.
I suppose that your Read_from_file()
should become something like
void read_from_file(list<Passenger> & flist, string const & filename)
{
string firstN, lastN, dest;
ifstream input(filename.c_str());
while (input >> firstN >> lastN >> dest)
{
flist.push_back(Passenger(firstN, lastN, dest)); // for C++98
// flist.emplace_back(fistN, lastN, dest); // for C++11/14
}
// input.close(); no need for close: file closed by destructor
}
Suggestion: pass parameters by reference, and using const
, when possible.
Similarly, the insert()
can be adjusted in a simple way (in two different modes, depending on whether you're using a recent C++ compiler or pre C++11)
void insert(list<Passenger> & flist, string const & firstN,
string const & lastN, string const & dest)
{
flist.push_back(Passenger(firsN, lastN, dest)); // C++98 version
// flist.emplace_back(firsN, lastN, dest); // C++11/14 version
}
Less obvious how to check_reservation()
. And really dependent from the C++98/C++11 question.
I suppose that you want a check_reservation()
that want find a Passenger
giving first name, last name and destination.
In C++11 is fairly easy (supposing your Passenger
contains a firsN
, a lastN
and a dest
member)
bool check_reservation(list<Passenger> const & flist, string const & firstN,
string const & lastN, string const & dest)
{
return flist.cend() != find_if(flist.cbegin(), flist.cend(),
[&](Passenger const & p)
(return (fistN == p.fistN)
&& (lastN == p.lastN)
&& (dest == p.dest);));
}
In C++98 you don't have lambda functions so is more tricky. You have to construct a functor for comparison; something like
struct person_eq : std::unary_function <Passenger, bool>
{
string const & f;
string const & l;
string const & d;
person_eq (string const & f0, string const & l0, string const & d0)
: f(f0), l(l0), d(d0)
{}
bool operator() (Passenger const & p) const
{ return (p.firstN == f) && (p.lastN == l) && (p.dest == d); }
};
and your check_reservation()
should become something like
bool check_reservation(list<Passenger> const & flist, string const & firstN,
string const & lastN, string const & dest)
{
return flist.cend() != find_if(flist.cbegin(), flist.cend(),
person_eq(fistN, lastN, dest));
}
I repeat: this code is untested, so be careful.
p.s.: sorry for my bad English.
Edit
Sorry: if it's defined the operator==
for your Passenger
, your check_reservation()
can be really easy
bool check_reservation(list<Passenger> const & flist, string const & firstN,
string const & lastN, string const & dest)
{
return flist.cend() != find(flist.cbegin(), flist.cend(),
Passenger(firstN, lastN, dest));
}