fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. // Ref: http://w...content-available-to-author-only...s.com/reference/iterator/istreambuf_iterator/
  6. // Ref: http://w...content-available-to-author-only...s.com/reference/istream/istream/sentry/
  7. // Ref: http://w...content-available-to-author-only...s.com/reference/istream/istream/
  8.  
  9. istream& getline(istream& is, vector<char> &buffer)
  10. {
  11. std::istream::sentry s(is);
  12. if (s)
  13. {
  14. std::istreambuf_iterator<char> it(is);
  15. std::istreambuf_iterator<char> end;
  16.  
  17. const size_t grow = 64;
  18. buffer.reserve(grow);
  19.  
  20. while (it != end && *it != '\n')
  21. {
  22. if (buffer.size() == buffer.capacity() - 1)
  23. {
  24. buffer.reserve(buffer.size() + grow);
  25. }
  26.  
  27. buffer.push_back(*it++);
  28. }
  29. buffer.push_back('\0');
  30. }
  31.  
  32. return is;
  33. }
  34.  
  35. int main()
  36. {
  37. vector<char> line;
  38. while (getline(std::cin, line))
  39. {
  40. if (!line.empty())
  41. {
  42. cout << line.data() << endl;
  43. line.clear();
  44. }
  45. }
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 3464KB
stdin
line1 line 1 line1 line 1 1 1 1 111
line 2    line 2 222 2 2 222 
line 3 3 3 33 3
line 4 4 4 44444 4
stdout
line1 line 1 line1 line 1 1 1 1 111
line 2    line 2 222 2 2 222 
line 3 3 3 33 3
line 4 4 4 44444 4