鱼C论坛

 找回密码
 立即注册
查看: 2163|回复: 2

[技术交流] 自己写的string类,不太成熟(可以用自己的iostream)

[复制链接]
发表于 2014-2-15 11:41:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 andalousie 于 2014-2-23 09:45 编辑

我的iostream(ios.h)下面是我的string(str.h)
  1. #ifndef STD_STRING
  2. #define STD_STRING

  3. #include "ios.h"
  4. #include <string.h>

  5. void memmove(char *dest, char *src, size_t n)
  6. {
  7.     /* No need to do that thing. */
  8.     if (dest == src)
  9.         return;

  10.     /* Check for destructive overlap.  */
  11.     if (src < dest && dest < src + n) {
  12.         /* Destructive overlap ... have to copy backwards.  */
  13.         src += n;
  14.         dest += n;
  15.         while (n-- > 0)
  16.             *--dest = *--src;
  17.     } else {
  18.         /* Do an ascending copy.  */
  19.         while (n-- > 0)
  20.             *dest++ = *src++;
  21.     }
  22. }

  23. class out_of_range{
  24. public:
  25.         out_of_range()
  26.         {
  27.                 std::cerr << "Error: Invalid access of element. " << std::endl;
  28.         }
  29. };

  30. namespace std{
  31.         class string{
  32.         public:
  33.                 typedef size_t        size_type;
  34.                 typedef char          value_type;
  35.                 typedef const string& const_reference;
  36.                 typedef const char *  const_pointer;
  37.                
  38.             string(const_pointer str = "");                 //default constructor(value_type)
  39.             string(value_type);
  40.             string(const_reference);                  //assign constructor(string)
  41.             
  42.             string&    operator= (const_reference);         //operator=
  43.             string     operator+ (const_reference) const;   //operator+
  44.             string&    operator+=(const_reference);         //operator+=
  45.             
  46.             //bool operators
  47.             bool       operator==(const_reference) const;    //operator==
  48.             bool       operator==(const_pointer) const;
  49.             bool       operator!=(const_reference) const;    //operator!=
  50.             bool       operator!=(const_pointer) const;
  51.             bool       operator< (const_reference) const;    //operator<
  52.             bool       operator< (const_pointer) const;
  53.             bool       operator> (const_reference) const;    //operator>
  54.             bool       operator> (const_pointer) const;
  55.             bool       operator<=(const_reference) const;    //operator<=
  56.             bool       operator<=(const_pointer) const;
  57.             bool       operator>=(const_reference) const;    //operator>=
  58.             bool       operator>=(const_pointer) const;
  59.             
  60.             value_type&      operator[](const size_type) const;  //operator[]
  61.             value_type&      at(const size_type n) const;
  62.             
  63.             size_type  size() const { return _len; }
  64.             size_type  length() const { return _len; }
  65.             string&    erase(size_type pos, size_type len);
  66.             void       swap(string& str);
  67.             void       clear();
  68.             bool       empty()  { return !_len; }
  69.             const_pointer c_str() const { return const_cast<char*>(m_data); }
  70.             ~string(void);
  71.         private:
  72.             value_type *m_data;
  73.             size_type  _len;
  74.             mutable bool local;
  75.         };
  76.         
  77.         inline string::string(const_pointer str)
  78.         {
  79.                 _len   = strlen(str);
  80.         m_data = new value_type[_len+1];
  81.         strcpy(m_data, str);
  82.         local  = true;
  83.         }
  84.         
  85.         inline string::string(value_type ch)
  86.         {
  87.             m_data = new value_type[1];
  88.         m_data[0] = ch;
  89.         _len   = 1;
  90.         local  = true;
  91.         }
  92.         
  93.         inline string::string(const_reference other)
  94.         {
  95.                 local  = false;
  96.             m_data = other.m_data;
  97.             _len   = other._len;
  98.         }
  99.         
  100.         inline string::~string(void)
  101.         {
  102.                 if(local) delete [] m_data;
  103.         }
  104.         
  105.         inline string& string::operator=(const_reference other)
  106.         {
  107.             if (this!=&other)
  108.             {
  109.                 string tmp(other);
  110.                 swap(tmp);
  111.             }
  112.             return *this;
  113.         }
  114.         
  115.         inline string string::operator+(const_reference other)const
  116.         {
  117.             string newstring;
  118.             newstring.local = false;
  119.                 newstring.m_data = new value_type[strlen(m_data)+strlen(other.m_data)+1];
  120.              strcpy(newstring.m_data,m_data);
  121.             strcat(newstring.m_data,other.m_data);
  122.             newstring._len = _len + other._len;
  123.              return newstring;
  124.         }
  125.         
  126.         inline string& string::operator+=(const_reference other)
  127.         {
  128.             *this = *this + other;
  129.             return *this;
  130.         }
  131.         
  132.         inline bool string::operator==(const_reference s) const
  133.         {
  134.              return s._len == _len
  135.                    && !strcmp(m_data,s.m_data);
  136.         }
  137.         
  138.         inline bool string::operator==(const_pointer s) const
  139.         {
  140.              return strlen(s) == _len
  141.                    && !strcmp(m_data,s);
  142.         }
  143.         
  144.         inline bool string::operator!=(const_reference s) const
  145.         {
  146.              return !(*this == s);
  147.         }
  148.         
  149.         inline bool string::operator!=(const_pointer s) const
  150.         {
  151.              return !(*this == s);
  152.         }
  153.         
  154.         inline bool string::operator< (const_reference s) const
  155.         {
  156.              return strcmp(m_data,s.m_data) < 0;
  157.         }
  158.         
  159.         inline bool string::operator< (const_pointer s) const
  160.         {
  161.              return strcmp(m_data,s) < 0;
  162.         }
  163.         
  164.         inline bool string::operator> (const_reference s) const
  165.         {
  166.              return strcmp(m_data,s.m_data) > 0;
  167.         }
  168.         
  169.         inline bool string::operator> (const_pointer s) const
  170.         {
  171.              return strcmp(m_data,s) > 0;
  172.         }
  173.         
  174.         inline bool string::operator<=(const_reference s) const
  175.         {
  176.              return !(*this > s);
  177.         }
  178.         
  179.         inline bool string::operator<=(const_pointer s) const
  180.         {
  181.              return !(*this > s);
  182.         }
  183.         
  184.         inline bool string::operator>=(const_reference s) const
  185.         {
  186.              return !(*this < s);
  187.         }
  188.         
  189.         inline bool string::operator>=(const_pointer s) const
  190.         {
  191.              return !(*this < s);
  192.         }
  193.                
  194.         inline char& string::operator[](const size_type n) const
  195.         {
  196.              if (n<0 || n>=_len) throw out_of_range();
  197.             return m_data[n];
  198.         }
  199.         
  200.         inline char& string::at(const size_type n) const
  201.         {
  202.              if (n<0 || n>=_len) throw out_of_range();
  203.             return m_data[n];
  204.         }
  205.         
  206.         inline string& string::erase(size_type pos, size_type len)
  207.         {
  208.                 if (pos >= _len || 0 == len) return *this;
  209.                 const size_type remainder = pos + len;
  210.                 if (remainder >= _len || remainder < pos)
  211.                 {
  212.                 *(m_data + pos) = '\0';
  213.                 _len = pos;
  214.                 return *this;
  215.             }
  216.             size_type left = _len - remainder + 1;
  217.             value_type *d  = m_data + pos;
  218.             value_type *s  = m_data + remainder;
  219.             memmove(d, s, left);
  220.             _len -= len;
  221.             return *this;
  222.         }
  223.         
  224.         inline void string::swap(string& str)
  225.         {
  226.                 string tmp(str);
  227.                
  228.                 str.local  = local;
  229.                 str.m_data = m_data;
  230.                 str._len   = _len;
  231.                
  232.                 local  = tmp.local;
  233.                 m_data = tmp.m_data;
  234.                 _len   = tmp._len;
  235.         }
  236.         
  237.         inline void string::clear()
  238.         {
  239.                 m_data = new value_type[1];
  240.                 strcpy(m_data, "");
  241.                 _len   = 0;
  242.         }
  243.         
  244.         istream& operator>>(istream& is, string& str)
  245.         {
  246.             char buffer[1000];
  247.             is >> buffer;
  248.             str = buffer;
  249.              return is;
  250.         }
  251.         
  252.         istream & getline(istream & is, string & str)
  253.         {
  254.             char buffer[1000];
  255.             is.getline(buffer, 1000);
  256.             str = buffer;
  257.             return is;
  258.         }
  259.         
  260.         ostream& operator<<(ostream& os, string& str)
  261.         {
  262.             os << str.c_str();
  263.             return os;
  264.         }
  265.         
  266. } //namespace std

  267. #endif
复制代码

测试文件test_string.cpp
  1. #include "str.h"
  2. using namespace std;

  3. int main()
  4. {
  5.         string s1;
  6.         string s2("23345sghehst");
  7.         cout << s1.size();
  8.         cout << s2.size() << endl;
  9.         s2.erase(4, 3);
  10.         string s3 = s1 + s2;
  11.         cout << s3 << endl;
  12.         if(s1 >= s2) cout << 1;
  13. }
复制代码




小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-2-18 10:52:43 | 显示全部楼层
路过看看= =!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-19 18:09:13 | 显示全部楼层
路过请帮我检查一下代码好吗?我刚刚增添了几个成员函数。这是我写的最长的头文件了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 15:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表