fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <sstream>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. int main(int, char**){
  10.  
  11. vector<float> three_floats{1.2f, 4.5f, -0.5f};
  12. vector<char> storage;
  13. storage.reserve(sizeof(float)*three_floats.size());
  14.  
  15. for(auto number: three_floats){
  16. cout << "Storing: " << number << "\n";
  17. for(int i = 0; i < sizeof(float); ++i){
  18. storage.push_back(reinterpret_cast<char*>(&number)[i]);
  19. }
  20. }
  21.  
  22. string str(storage.begin(), storage.end());
  23.  
  24. cout << "str len " << str.length() << "\n";
  25.  
  26.  
  27. for(int i = 0; i < str.size(); i+=sizeof(float)){
  28. cout << "Reading " << *reinterpret_cast<float*>(&storage[i]) << "\n";
  29. }
  30.  
  31. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
Storing: 1.2
Storing: 4.5
Storing: -0.5
str len 12
Reading 1.2
Reading 4.5
Reading -0.5