Reading large data file into data structure c++
To give a little background i have three main classes Customer, Tours, Hires.
Customer will have basic customer data in its constructor such as customer name, customer ID.
Tour which is a base class for GuidedTours has similar data.
Hires is a class which has details for equipment which can be hired by customer
Main question
We are required to read data for all these classes from large text file, and basically store them in different data structure Map, List, Vector to do a performance bound checking on all these data structures.
So i can see it working but the part which is confusing me the most is how do i initialise different objects
1 if i read it from main, tokenise the line as follow
Tour *newtour = new Tour();
newTour.addtoMap(tokenise, data, will, go, here);
In this method i will add function called "addtoMap" in tour and have my data structure implemented in it, and on the main.cpp side i will save every Tour pointer in vector. Do i have to initialise data structure in the default Tour() constructor.
And is this a good programming practice? And we required to do a time on every data structure to be able to give graphical presentation and deviation between different structure, but i will be using vector for every to store all my Tour, Customer, Hire pointer in main.cpp, will that have any significant effect on performance.
2 If i do in the following way
Tour *newtour = new Tour(tokenised, data, goes, here);
this way i am passing value to constructor, so i will have to perform my Map implementation in that constructor which i feel like is not good.
Is there any other way for me to go about this, i like this method but i cannot visualise how and where i will implement my Map to store that data?
I am new to c++, i want to implement it in a way that the reason for doing it that way is to support OOP methodology. If there is any other way apart i am open for that.
I had looked at another thread with similar problem, but he had 3.8gb data file mine is only 934 kb but its large enough for me.
You need to separate between container and data object - it sounds like what you need are "simple" dataholder classes for your data objects, eg one Customer
class. And, for each of these, you will then need a container holding objects of this class.
You best use the STL (standard template library) containers, like vector
(or map
, or whatever you need), eg vector<Customer>
. These containers will do all the heavy lifting for you regarding management of the list. You only have to write your data classes, eg:
class Customer
{
private:
std::string m_name;
public:
Customer(std::string name): m_name(name) {}
};
And then:
std::string nameLoadedFromFile;
Customer customer(nameLoadedFromFile);
std::vector<Customer> custList.push_back(customer);
My recommendation if you want to read from text file into these classes is that you provide a constructor for input stream and operators to serialize data for each of your classes. The c++ way to do this is usually like this:
class Tour {
public:
Tour(std::istream & input);
friend std::ostream & operator<<(sd::ostream & channel, Tour const & t);
friend std::istream & operator>>(std::istream & channel, Tour & t);
};
EDIT: The order of parameters was not correct, I changed it. Sorry for my mistake.
Take a look at this tutorial for learning about streams. operator<< must be able to write your classes to a std::stream
. operator>> must be able to read it. The stream constructor is a good idea if it makes no sense to default-construct one of your classes, but as a minimum, provide operator<< and operator>>.
Once you can read/write your classes, putting them into a map
or vector
is a matter of reading the file and using these methods.
Using pointers is bad programming practise. What's wrong with a vector of Tours instead of a vector of Tour pointers?
To me the answer to your question is to change the way you add the tour to the map. This looks like the most natural way to do things
Tour newtour(tokenised, data, goes, here); // no pointers
map.addTour(newtour);
So instead of addtoMap as a method of Tour, you have addTour as a method of your map object.
链接地址: http://www.djcxy.com/p/87902.html上一篇: 如何将来自C / C ++的数据结构编组成python并返回
下一篇: 将大数据文件读入数据结构c ++