I'm about to start a new project about processing data stored in text file (all data are of type double) and I'm wondering which appraoch is the best (in terms of execution time) to deal with this huge amount of data:
-Pointers associated with dynamic memory allocation?
Also, what is the difference between the two approaches and why?
I would say the best way (in terms of execution time) is to use a std::multiset<double>
multiset because multyset allows you to store numbers which may be equal, and because we (probably) don't know what in the text file is we use multiset.
If you're sure that all the numbers are different then std::set is prefered because it allows you to store only unique numbers.
Second thig why multiyset/set is prefered is the fact that set sort's it members during inserting and because you deal with huge amount of data, searching trough the set is extremly fast.
and of course set like any other STL container has a methods that enables you to perform various tasks.
also you can use STL algorhtms etc etc...
one more thing about multiset is that you don't have to provide how many data you have, multiset will resize on it own as needed so you don't have to wory about memeory allocation.
You don't say what the data is or how it's going to be used. If the order of the data is important then std::set/std::multiset is not very good.
Using a static array could be a problem because you probably don't know how much data the files will contain at compile time, and you can't change the size at runtime. If the amount of data is very large you easily run out of stack memory.
If you dynamically allocate the array with new you can decide the array size at runtime. If you don't know the amount of data when you start reading you will have to implement a mechanism to allocate more memory for your data in case you run out of allocated memory. If you use std::vector you don't need to worry about that. You can just use the push_back member function and it will automatically allocate more memory when needed.
-Pointers associated with dynamic memory allocation?"
Pointers are the underlying type for all containers & arrays. Manually handling pointers increases the possibility of mistakes, such as memory leaks, segmentation faults, and access violations. The standard containers, such as std::vector, encapsulate the underlying pointers, which separates you from the pointers.
The choice depends entirely on the intent. If your program simply stores a group of something, with no specific requirements, use a standard container. If your program requires specific placement of objects, or requires careful alignment, use manual allocations.
hello again everybody,,,,,I 'm very grateful for you and your discussion it has ben so instructive,,,,I think I have been mixing things, but now it's clearer to me, and I like too much the idea of using std::vector, I think they'll stisfy my need to speed of execution;
if I had well understood, the STL containers are the product of pointers use, isn't it?
If it's the case, then I think I'm getting better :)
"if I had well understood, the STL containers are the product of pointers use, isn't it?"
Yes. As I've said, the std::vector isn't always the best solution. Sometimes, things have to be custom made to tailor your needs. I do this all too often, and I enjoy it, but it's experience at the end of the day.