-
Disk IO Speed
ok, i got an object, call him bob. im looking for the fastest way to populate a stl container like vector or map of bobs from a file. i was told a while ago i could use the copy function to directly load the entire file into the vector, however im unclear of how to do this, or the rules governing the copy. is this the fastest way? is it faster than looping reading each one individually? copy(ifstream.begin(),ifstream.end(),vec_bob.begin ()); <-would something like that be what im looking for? Thanks for the help.
-
The most usual way this is accomplished is to use istream_iterator 's .
Scott meyers wrote an article in his more effective c++ book called " be alert for c++'s most vexing parse" that shows how to do this and the pitfalls of getting it wrong.
std:: omitted for brevity.
Code:
ifstream file("file.dat");
vector<int> bob( (istream_iterator<int>(file)), istream_iterator<int>());
This is technically correct.
this below isn't....
Code:
ifstream file("file.dat");
vector<int> bob(istream_iterator<int>(file),istream_iterator<int>());
Can you work out why the first should work according to the c++ standard and yet the second will not. To be portable between compilers tho it is often better coded like this...
Code:
ifstream file("file.dat");
istream_iterator<int> filebegin(file);
istream_iterator<int> fileend;
vector<int> bob(filebegin,fileend);
Of course this just illustrates range construction from a file. If the vector is precreated you can use vector::insert() instead.
-
thats an interesting way to do it, i havent seen that way before. how is it speed wise? im worried because im potentially loading a very large dictionary of bob's, and execution time is a major consideration for this particular gadget. im just worried about loading 10,000 bobs and taking 5 minutes to read and load them.
-
I just tried those and am getting the error:
error C2664: '__thiscall std::vector<int,class std::allocator<int> >::std::vector<int,class std::allocator<int> >(unsigned int,const int &,const class std::allocator
<int> &)' : cannot convert parameter 1 from 'class std::istream_iterator<int,char,struct std::char_traits<char> >' to 'unsigned int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
-
why are you using vector<int> surely you would want vector<bob> or whatever your class is anyway go here and get this because you obviously are gonna be needing it.
-
Quote:
Originally posted by Stoned_Coder
anyway go here and get this because you obviously are gonna be needing it.
There is a God! I had no idea such a thing existed...
-
Yep and he smokes pot!!! :D :D :D
-
int was just a simple example of a datatype. i would like to get it working with a simple one like int before i try a complicated class like my bob. and yes, i have a feeling ill be using that error decoder ALOT. thanks for posting it. you should put it in the FAQ i never took the time to read =P