fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. struct Tabular {
  5. size_t width;
  6. std::ios_base&(*flag)(std::ios_base&);
  7. Tabular(): width(10), flag(std::left) {}
  8. template<typename T>
  9. Tabular& operator<<(const T& data) {
  10. std::cout << flag << std::setw(width) << data;
  11. return *this;
  12. }
  13. Tabular& operator<<(const char& ch) {
  14. if(ch == '\n' || ch == '\t')
  15. std::cout << ch;
  16. else
  17. std::cout << flag << std::setw(width) << ch;
  18. return *this;
  19. }
  20. Tabular& operator<<(std::ostream&(*out)(std::ostream&)) {
  21. std::cout << out;
  22. return *this;
  23. }
  24. };
  25.  
  26.  
  27.  
  28. int main() {
  29. Tabular out;
  30. out.width = 20;
  31. out << "Person 1" << 1.2 << "Text" << '\n';
  32. out << "Person 2" << 10 << "Text 2" << std::endl;
  33. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
Person 1            1.2                 Text                
Person 2            10                  Text 2