Chinaunix首页 | 论坛 | 博客
  • 博客访问: 271420
  • 博文数量: 84
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-06 23:00
个人简介

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: C/C++

2015-12-11 14:03:27

用动态顺序表实现

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include<windows.h>
  3. #include<assert.h>
  4. using namespace std;
  5. class String
  6. {
  7. public:
  8.     String(const char* str = "")
  9.     {
  10.         int len = strlen(str);
  11.         _capacity = len +1;
  12.         _size = len;
  13.         _str = new char[_capacity];
  14.         strcpy(_str, str);
  15.     }
  16.     String(const String& s)
  17.         :_str(new char[1]())
  18.     {
  19.         String tmp = s._str;
  20.         StringSwap(tmp);
  21.     }

  22.     String& operator=(String s)
  23.     {
  24.         StringSwap(s);
  25.         return *this;
  26.     }

  27.     ~String()
  28.     {
  29.         if (_str)
  30.         {
  31.             delete[] _str;
  32.             _str = NULL;
  33.         }
  34.     }
  35.     void Display()
  36.     {
  37.         cout<<_str<<endl;
  38.     }
  39.     bool operator>(const String& s)
  40.     {
  41.         return (MyStrcmp(_str,s._str) > 0);
  42.     }
  43.     bool operator<(const String& s)
  44.     {
  45.         return (MyStrcmp(_str,s._str) < 0);
  46.     }
  47.     bool operator>=(const String& s)
  48.     {
  49.         return (MyStrcmp(_str,s._str) >= 0);
  50.     }
  51.     bool operator<=(const String& s)
  52.     {
  53.         return (MyStrcmp(_str,s._str) <= 0);
  54.     }
  55.     bool operator==(const String& s)
  56.     {
  57.         return (MyStrcmp(_str,s._str) == 0);
  58.     }
  59.     void PushBack(char ch)
  60.     {
  61.         int len = 1;
  62.         _size += len;
  63.         _str = _CheckCapacity();
  64.         
  65.         _str[_size - 1] = ch;
  66.     }
  67.     void Insert(size_t pos,char ch)
  68.     {
  69.         assert(pos < _size);
  70.         int len = 1;
  71.         _size += len;
  72.         _str = _CheckCapacity();

  73.         for(size_t i = _size;i >= pos;--i)
  74.         {
  75.             _str[i] = _str[i - 1];    
  76.         }
  77.         _str[pos -1] = ch;
  78.     }
  79.     void Insert(size_t pos,const char* str)
  80.     {
  81.         assert(pos <= _size);
  82.         int len = strlen(str);
  83.         _size += len;
  84.         _str = _CheckCapacity();

  85.         for(size_t i =_size;i >= pos;i--)
  86.         {
  87.             _str[i] = _str[i - len];
  88.         }
  89.         for(size_t i = 0;i < len;i++)
  90.         {
  91.             _str[pos + i] = str[i];
  92.         }
  93.     }
  94.     char& operator[](size_t index)
  95.     {
  96.         return _str[index];
  97.     }
  98.     int Find(char ch)
  99.     {
  100.         for(size_t i = 0;i<_size;i++)
  101.         {
  102.             if(_str[i] == ch)
  103.             {
  104.                 return i;
  105.             }
  106.         }
  107.         return -1;
  108.     }
  109.     int Find(const char *str)//"hello world" "wor" ->6
  110.     {
  111.         const char *pscr = _str;
  112.         const char *psub = str;

  113.         int scrlen = _size;
  114.         int sublen = strlen(str);

  115.         int scrIndex = 0;
  116.         while(scrIndex <= scrlen - sublen)
  117.         {
  118.             int i = scrIndex;
  119.             int j = 0;
  120.             while(scrlen > i && j < sublen && pscr[i] == psub[j])
  121.             {
  122.                 i++;
  123.                 j++;
  124.             }
  125.             if(j == sublen)
  126.             {
  127.                 return scrIndex;
  128.             }
  129.             ++scrIndex;
  130.         }
  131.         return -1;
  132.     }
  133.     void Erase(size_t pos)
  134.     {
  135.         assert(pos < _size);
  136.           for(size_t i = pos;i<_size;i++)
  137.         {
  138.             _str[i] = _str[i + 1];
  139.         }
  140.         _size--;
  141.     }
  142.     void Erase(size_t pos,size_t n)
  143.     {
  144.         assert(pos < _size - n);
  145.           for(size_t i = pos;i<_size;i++)
  146.         {
  147.             _str[i] = _str[i + n];
  148.         }
  149.         _size -= n;
  150.     }
  151. private:
  152.     char *_CheckCapacity()
  153.     {
  154.         if(_size >= _capacity)
  155.         {
  156.             size_t NewCapacity = (_capacity * 2 > (_size + 1) )? _capacity * 2:(_size+1);//这里之所以要加1是因为当插入一个字母时,_size与_capacity相等,要是不开辟多余的一个空间 ,那么那个/0就不能往后移动,会越界。
  157.             _str = (char *)realloc(_str,NewCapacity);
  158.             _capacity = NewCapacity;
  159.             return _str;
  160.         }
  161.         return _str;
  162.     }
  163.     void StringSwap(String &s)
  164.     {
  165.         swap(_str,s._str);
  166.         swap(_size,s._size);
  167.         swap(_capacity,s._capacity);        
  168.     }
  169.     int MyStrcmp(const char *str1,const char *str2)
  170.     {
  171.         while((*str1++ == *str2++))
  172.         {
  173.             if(*str1 =='\0')
  174.             {
  175.                 return 0;
  176.             }
  177.         }
  178.         if(*str1 > *str1)
  179.         {
  180.             return 1;
  181.         }
  182.         else
  183.         {
  184.             return -1;
  185.         }
  186.     }
  187.     char* _str;
  188.     size_t _capacity;
  189.     size_t _size;
  190. };

  191. void Test1()
  192. {
  193.     String s1("hello world");
  194.     s1.Display();
  195.     String s2(s1);
  196.     s2.Display();
  197.     String s3("change world");
  198.     s1 = s3;
  199.     s1.Display();
  200. }
  201. void Test2()
  202. {
  203.     String s1("ello world");
  204.     s1.Insert(1,'H');
  205.     s1.Display();
  206. }
  207. void Test3()
  208. {
  209.     String s1("hellorld");
  210.     char *str = " wo";
  211.     s1.Insert(5,str);
  212.     s1.Display();
  213. }
  214. void Test4()
  215. {
  216.     String s1("Hello");
  217.     cout<<s1[0]<<endl;
  218. }
  219. void Test5()
  220. {
  221.     String s1("hello world");
  222.     cout<<s1.Find("ll")<<endl;
  223. }
  224. void Test6()
  225. {
  226.     String s1("hello world");
  227.     s1.Erase(2,3);
  228.     s1.Display();
  229. }
  230. int main()
  231. {
  232.     //Test1();
  233.     //Test2();
  234.     //Test3();
  235.     //Test4();
  236.     //Test5();
  237.     Test6();
  238.     system("pause");
  239.     return 0;
  240. }


阅读(1408) | 评论(0) | 转发(0) |
0

上一篇:用operator new实现new

下一篇:String:COW

给主人留下些什么吧!~~