fork download
  1. #include <string>
  2. #include <sstream>
  3. #include <iostream>
  4.  
  5. template <class Number>
  6. std::string& operator<<(std::string& s, Number a) {
  7. return s += std::to_string(a);
  8. }
  9. std::string& operator<<(std::string& s, const char* a) {
  10. return s += a;
  11. }
  12. std::string& operator<<(std::string& s, const std::string &a) {
  13. return s += a;
  14. }
  15. int main() {
  16. std::string s;
  17. // this prints out: "inserting text and a number(1)"
  18. std::cout << (s << "inserting text and a number (" << 1 << ")\n");
  19.  
  20. // normal way
  21. std::ostringstream os;
  22. os << "inserting text and a number (" << 1 << ")\n";
  23. std::cout << os.str();
  24. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
inserting text and a number (1)
inserting text and a number (1)