uint64_t binary file r/w and efficiency question

Hi guys I need to read/write two vectors of uint64_t to a binary file. I've been reading how to do this and have seen some example code for other data structures. But I am still not clear on how to do it. If someone could write code that could do this, it would really help the learning process.

Also, I am considering representing these pairs of integers using the following data structure:
 
vector< pair<uint64_t, uint64_t> > intPairs;


I would be accessing the numbers many times, on the order of millions. Would I lose a significant amount of efficiency by implementing this data structure as opposed to just two separate vectors? If no, perhaps you'd be nice and provide example code using that data structure? Thank you!
Last edited on
Would I lose a significant amount of efficiency by implementing this data structure

With a good optimizing compiler, I'm not sure it's going to make much of a difference.

With two vectors:
1
2
3
4
5
 
  vector<uint64_t> va; 
  vector<uint64_t> vb;
  bf.write (&va[i], sizeof(uint64_t)); 
  bf.write (&vb[i], sizeof(uint64_t)); 


With pair<>:
1
2
3
4
 
  vector< pair<uint64_t, uint64_t> > pairs;
  bf.write (&pairs[i].first, sizeof(uint64_t)); 
  bf.write (&pairs[i].second, sizeof(uint64_t)); 


Keeping in mind that pair<> is simply a templated struct, I'd be inclined to write this as follows:
1
2
3
4
 
  vector< pair<uint64_t, uint64_t> > pairs;
  const size_t pair_size = sizeof(pair<uint64_t, uint64_t>);
  bf.write (&pairs[i], pair_size); 


Since vectors are stored contigously, you could get away with the following to write everything at once:
1
2
3
4
 
  vector< pair<uint64_t, uint64_t> > pairs;
  const size_t pair_size = sizeof(pair<uint64_t, uint64_t>);
  bf.write (&pairs[0], pair_size * pairs.size()); 


Keep in mind, that your file format won't be portable across platforms, if the platforms have different endiness.







Last edited on
Topic archived. No new replies allowed.