Advertisement
pileon

Vector input/output

Feb 20th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <fstream>
  4. #include <vector>
  5.  
  6. int main()
  7. {
  8.     {
  9.         std::vector<long> v = { 1, 2, 3, 4 };
  10.  
  11.         std::ofstream out("/tmp/test.bin", std::ios::binary);
  12.  
  13.         std::for_each(std::begin(v), std::end(v), [&out](const long& v) {
  14.             out.write(reinterpret_cast<const char*>(&v), sizeof(v));
  15.         });
  16.     }
  17.  
  18.     {
  19.         std::vector<long> v(4);
  20.  
  21.         std::ifstream in("/tmp/test.bin", std::ios::binary);
  22.  
  23.         std::for_each(std::begin(v), std::end(v), [&in](long& v) {
  24.             in.read(reinterpret_cast<char*>(&v), sizeof(v));
  25.         });
  26.  
  27.         for(const auto& n : v)
  28.             std::cout << n << '\n';
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement