如何访问加载在列表容器中的文件流中的变量

How to Access a variable in a filestream loaded in list container

本文关键字:文件 变量 列表 何访问 访问 加载      更新时间:2023-10-16

我是新来的,正在学习c++。我在列表容器中加载了一个文件流,使用变量。我希望能够访问并改变这些变量的值。我已经试了几个星期了,但毫无效果。有人能帮忙吗?

这是外部文本文件:flightBoard1.txt

  Delta 3431 Paris JFK
  Usair 2275 EWR London
  Delta 1500 Bonn Milan

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <list>
using namespace std;
template<class T, class U, class V>
void changeFlight(list<string> &myFlight, T loc, U value, V newVal);
int main()
{
    string _company;
    int _flight;
    string _origin;
    string _destination;
    list<string> flightBoard;
    stringstream *ssPtr;
    int counter = 0;
    ifstream myFile("flightBoard1.txt");
    while(myFile >> _company >> _flight >> _origin >> _destination ){
        ++counter;
        ssPtr = new stringstream; // you want to put each line in a different slot in the flightBoard list object
        *ssPtr << counter << " " << _company << "t" << _flight << "t" << _origin << "t" << _destination << endl;
        flightBoard.push_back(ssPtr->str()); // You need an arrow, this is a pointer
    }
    list<string>::iterator it;
    for(it = flightBoard.begin(); it != flightBoard.end(); it++){
        cout << *it ;
    }
    int oldFlight, newFlight;
    cout << endl << "Enter old flight number: ";
    cin >> oldFlight;
    cout << "Enter new flight number: ";
    cin >> newFlight;
    changeFlight(flightBoard, ssPtr, oldFlight, newFlight);
    delete ssPtr;
    myFile.close();

    return 0;
}
template<class T, class U, class V>
void changeFlight(list<string> &myFlight, T loc, U value, V newVal){
    list<string>::iterator it;
    cout << endl << "Flight: " << value << " has been changed to: " << newVal << endl;
    for(it = myFlight.begin(); it != myFlight.end(); it++){
        // THIS IS WHERE I AM HAVING A PROBLEM
        // PLEASE UN-COMMENT BELOW TO SEE PROBLEM
        /*if(it -> myFlight -> loc -> value){
            value = newVal;
        }*/ 
    }
}

为了解决您的问题,我认为您应该使用更好的结构来存储您的航班。如果需要进一步操作数据,string不是一个好类型。我建议引入Flight类:

class Flight
{
  public:
    string company;
    int flight;
    string origin;
    string destination;
    Flight(const string& _company, int _flight, const string& _origin, const string& _destination)
      : company(_company), flight(_flight), origin(_origin), destination(_destination)
    {
    }
    string ToString() const
    {
      stringstream printer;
      printer << company << "t" << flight << "t" << origin << "t" << destination << endl;
      return printer.str();
    }
}; 

也有一些问题的代码片段,你已经发布。

  • while循环内存泄漏。您在每次迭代中分配ssPtr = new stringstream;,但delete只在最后分配一次。
  • changeFlight有太多模板类型参数。如果U value应该在changeFlight中改为V newVal,那么它应该具有相同的类型。
  • 很难改变flightBoard的类型,list<string>到处复制。最好为list<string>类型创建typedef,以使您的代码更简单。例:typedef list<string> FlightListType; .

下面是修复了上述所有问题的代码:

typedef list<Flight> FlightListType;
template<class T>
void changeFlight(FlightListType& myFlight, T value, T newVal);
int main()
{
    string _company;
    int _flight;
    string _origin;
    string _destination;
    FlightListType flightBoard;
    ifstream myFile("flightBoard1.txt");
    while(myFile >> _company >> _flight >> _origin >> _destination )
    {
      flightBoard.push_back(Flight(_company, _flight, _origin, _destination)); 
    }
    FlightListType::const_iterator it;
    int counter = 0;
    for(it = flightBoard.begin(); it != flightBoard.end(); it++)
    {
      cout << counter << " " << (*it).ToString();
      ++counter;
    }
    int oldFlight, newFlight;
    cout << endl << "Enter old flight number: ";
    cin >> oldFlight;
    cout << "Enter new flight number: ";
    cin >> newFlight;
    changeFlight(flightBoard, oldFlight, newFlight);
    myFile.close();
    return 0;
}
template<class T>
void changeFlight(FlightListType& myFlight, T value, T newVal)
{
    FlightListType::iterator it;
    cout << endl << "Flight: " << value << " has been changed to: " << newVal << endl;
    for(it = myFlight.begin(); it != myFlight.end(); it++)
    {
      if ((*it).flight == value)
      {
        // TODO: Here you can do with the Flight what ever you need
        // For example change it's number
        (*it).flight = newVal;
      }
    }
}