如何访问加载在列表容器中的文件流中的变量
How to Access a variable in a filestream loaded in list container
我是新来的,正在学习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;
}
}
}
相关文章:
- 通过多个头文件使用常量变量
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 无法通过头文件保存变量的值
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在 C++ 中将双精度变量写入二进制文件
- 如何使用文件中的文本,例如变量
- 定义C++新的环境变量并在 bat 文件中使用它
- 在头文件中定义变量不会出错
- 复制文件流C++静态变量
- 从另一个 cpp 文件更改结构内、映射键内的变量
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- C++将文本从.txt文件解析为变量
- 从 C 中的变量获取文件的路径,C++
- 使用boost ::日志配置文件变量
- C++:帮助存储和显示输入文件变量
- 文件变量的问题
- 如何在先决条件列表中展开生成文件变量
- QT多个文件c++变量