fork(10) download
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. std::stringstream::pos_type size_of_stream(const std::stringstream& ss)
  5. {
  6. std::streambuf* buf = ss.rdbuf();
  7.  
  8. // Get the current position so we can restore it later
  9. std::stringstream::pos_type original = buf->pubseekoff(0, ss.cur, ss.out);
  10.  
  11. // Seek to end and get the position
  12. std::stringstream::pos_type end = buf->pubseekoff(0, ss.end, ss.out);
  13.  
  14. // Restore the position
  15. buf->pubseekpos(original, ss.out);
  16.  
  17. return end;
  18. }
  19.  
  20. int main()
  21. {
  22. std::stringstream ss;
  23.  
  24. ss << "Hello";
  25. ss << ' ';
  26. ss << "World";
  27. ss << 42;
  28.  
  29. std::cout << size_of_stream(ss) << std::endl;
  30.  
  31. // Make sure the output string is still the same
  32. ss << "\nnew line";
  33. std::cout << ss.str() << std::endl;
  34.  
  35. std::string str;
  36. ss >> str;
  37. std::cout << str << std::endl;
  38. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
13
Hello World42
new line
Hello