fork download
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. template<size_t maxsize>
  5. class str {
  6. private:
  7. char buf[maxsize];
  8. public:
  9. str(void){}
  10. str(const str& s) { std::strcpy(buf, s.buf);}
  11. str(const char* s){ std::strcpy(buf, s);}
  12. public:
  13.  
  14. str& operator = (const str& s){
  15. if(this != &s)
  16. std::strcpy(buf, s.buf);
  17. return *this;
  18. }
  19.  
  20. str& operator += (const str& s){
  21. if(this != &s)
  22. std::strcat(buf, s.buf);
  23. return *this;
  24. }
  25.  
  26. friend str operator + (const str& s1, const str& s2){
  27. str o(s1);
  28. return o += s2;
  29. }
  30.  
  31. friend std::istream& operator >> (std::istream& _in, str& s){
  32. return _in.get(s.buf, maxsize - 1);
  33. }
  34.  
  35. friend std::ostream& operator << (std::ostream& _out, const str& s){
  36. return _out << s.buf;
  37. }
  38.  
  39. bool operator < (const str& s) const {
  40. return (std::strcmp(buf, s.buf) < 0);
  41. }
  42.  
  43. bool operator > (const str& s) const {
  44. return (std::strcmp(buf, s.buf) > 0);
  45. }
  46.  
  47. bool operator == (const str& s) const {
  48. return (! std::strcmp(buf, s.buf));
  49. }
  50.  
  51. const char* get(void) const { return &buf[0]; }
  52. };
  53.  
  54.  
  55. int main(void){
  56. str<64> s;
  57. std::cout << "Enter str: ";
  58. std::cin >> s;
  59. std::cin.sync();
  60.  
  61. if(s == "exit")
  62. return 0;
  63.  
  64. s = "-[" + s + "]-";
  65. s += "\tfin";
  66. std::cout << s << std::endl;
  67.  
  68. str<64> p = s;
  69. if(p == s)
  70. std::cout << "yes cmp.\n";
  71. std::cin.get();
  72. return 0;
  73. }
Success #stdin #stdout 0s 3416KB
stdin
hello end
stdout
Enter str: -[hello end]-	fin
yes cmp.