C ++:将文本文件中的数据存储在STL指针向量中 [英] C++: Storing data from text file in a STL Vector of pointers

查看:59
本文介绍了C ++:将文本文件中的数据存储在STL指针向量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经获得并完成了关于储蓄俱乐部的任务。俱乐部有两种成员类型(Full和Young)。我已经为每个成员类型编写了一个带有虚函数的abstact(Member)类和两个类(Full和Young)。我想我已经设法编写了我的所有功能,但是程序的一部分似乎无法理解。我的任务要求我使用C ++ STL指针向量。这是我不明白的。我已经设置了我的程序,以便我的程序将成员数据写入文本文件。我想知道的是如何获取文本文件的这些数据并存储到指针向量中以便在我的程序函数中使用。如果你愿意,我可以上传我的代码。我文件中的文字存储为(姓名,帐号,余额,年龄,日期)


我编译代码时的另一件事似乎并不是对日期感到高兴声明。

I have been given and assignment to complete which is about a savings club. The club has two member types (Full and Young). I have written a abstact (Member) class with virtual functions and two classes (Full and Young) for each member type. I think I have managed to write all of my functions but there is one part of the program that I cannot seem to understand. My assignment asks for me to use a C++ STL Vector of pointers. This is what I don''t understand. I have set up my program so that my program writes the members data to a text file. What I would like to know is how do I go about getting this data of the text file and stored into a vector of pointers for use in my programs functions. I can upload my code so far if you want. Text in my file is stored as (name, account number, balance, age, date)

One other thing when I compile my code it does not seem to be to happy about the date declaration.

推荐答案


我的作业要求我使用C ++ STL指针向量。
My assignment asks for me to use a C++ STL Vector of pointers.



创建一个基本指针向量std :: vector< Member *>,在该向量中,您可以存储派生类Young *和Full *的指针。

Create a vector of base pointers std::vector<Member*>, in that vector you can store the pointers of derived classes Young* and Full*.


我想知道的是如何获取文本文件的这些数据并存储到指针向量中以便在我的程序函数中使用。
What I would like to know is how do I go about getting this data of the text file and stored into a vector of pointers for use in my programs functions.



您是否能够从文本文件中读取数据,解析条目并使用它们来创建成员派生对象?如果是这样,那么你几乎就在那里。

Are you able to read the data from the text file, parse the entries and use them to create Member-derived objects? If so then you''re nearly there.


当我编译我的代码时,另一件事似乎并不是对日期声明感到高兴。
One other thing when I compile my code it does not seem to be to happy about the date declaration.



什么日期声明?

What date declaration?


感谢您的回复,所以在我的主要功能中我想:


从文件中读入数据


创建一个像这个向量的向量< Member *> //我正在使用命名空间std所以不需要std :: ????

如何将文件中的数据导入向量中,如何从该向量中存储派生类的向量(如何让它向量知道我正在使用哪个派生类)


将数据传递给函数


清除并删除向量。


我写了一个指向函数的菜单,我如何在我的向量中使用它。


BTW你打算传递条目?
Thanks for the reply, so in my main function I would want to:

Read in data from file

Create a vector like this vector<Member*>//i am using namespace std so no need for std::???

How do I go about getting the data from the file into the vector and how do I go about storing the vectors of the derived classes from that vector (how do I let the vector know which derived class I am using)

Pass the data to functions

Clear and delete vectors.

I have written a menu which points to the functions, how would I use this with my vector.

BTW where you meant to say pass the entries?



创建一个像这个向量的向量< Member *> //我正在使用命名空间std所以不需要std :: ???
Create a vector like this vector<Member*>//i am using namespace std so no need for std::???



这是真的,但通常认为不要''使用命名空间std',而是在任何东西上使用std ::但是最小的项目。 std命名空间在这里有一个原因,你很快就会很快输入它。

That''s true but it''s generally regarded as good practice to not to ''using namespace std'' but to use std:: instead on anything but the smallest projects. The std namespace is here for a reason, you''ll get pretty quick at typing it soon.


如何将文件中的数据转换为向量以及如何从该向量存储派生类的向量(如何让向量知道我正在使用哪个派生类)
How do I go about getting the data from the file into the vector and how do I go about storing the vectors of the derived classes from that vector (how do I let the vector know which derived class I am using)



'是使用基指针向量的关键。一个Full 是一个成员,所以它可以进入Member * s的向量,向量不知道你正在存储Full *或Young *,只有你是存储会员*。如果在向量的元素上调用虚拟Member *方法恰好是Full *,则将调用派生类Full *的相应方法。你只需要一个向量来完成工作,没有矢量等矢量。

That''s the point of using a vector of base pointers. A Full is a Member, so it can go in a vector of Member*s, the vector doesn''t know that you''re storing Full* or Young*, only that you''re storing Member*s. If you call a virtual Member* method on an element of your vector that happens to be a Full*, then the appropriate method of the derived class Full* will be called. You only need one vector to do the job and no vector of vectors, etc.


我写了一个指向函数的菜单,我将如何使用它我的载体。
I have written a menu which points to the functions, how would I use this with my vector.



如果函数是虚基类方法,你可以在你的成员向量上调用它们:members [i] - > thefunction()

If the functions are virtual base class methods, you call them on your vector of members like this: members[i]->thefunction()


BTW你打算通过条目?
BTW where you meant to say pass the entries?



我没说过,我说解析。当您阅读文本文件时,您必须将结果字符串转换为用于构造成员派生对象的整数和其他数据。这称为解析字符串。

I didn''t say pass, I said parse. When you read your text file you''ll have to turn the resulting strings into integers and other data which you use to construct Member-derived objects. This is called "parsing the string".


这篇关于C ++:将文本文件中的数据存储在STL指针向量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆