c++模拟实现string

来源:互联网 发布:mac windows 双系统 编辑:程序博客网 时间:2024/06/06 04:56
#define  _CRT_SECURE_NO_WARNINGS 1#include <iostream> using namespace std; class String { public: String(const char* str=" ") {        size_t len=strlen(str);_str=new char[len+1];strcpy(_str,str);_size=len;_capacity=len; } String(const String& s) :_str(NULL) { String tmp(s._str); tmp.Swap(*this);  } String& operator=(String s) {         this->Swap(s); return *this; }     ~String() {     if (_str)     { delete [] _str; _str=NULL; _size=_capacity=0;     } } char* Getstr() {   return _str; } const char* Getstr() const {      return _str; } size_t Size() {      return  _size; } size_t Size() const {       return _size; }     size_t capacity() {   return _capacity; } void Expand(size_t n) {    assert(n>_capacity);_str=(char*)realloc(_str,n);_capacity=n; } void PushBack(char ch) {    if (_size==_capacity)    {Expand(_size*2);    }_str[_size]=ch;_str[_size+1]='\0';++_size; } void Insert(size_t pos,const char* str) {   assert(pos<=_size);   size_t len=strlen(str);   if (_size+len>_capacity)   {   Expand(_size+len+1);   }       size_t end=_size;  while (end>=pos)  {  _str[len+end]=_str[end];          --end;  }  while (*str)  {  _str[pos]=*str;  ++pos;  ++str;  } } void Insert(size_t pos, char ch)  { assert(pos <= _size); size_t end = _size; while(end >= pos) { _str[end+1] = _str[end]; --end; } _str[pos] = ch; ++_size; } void Insert(size_t pos,const String& s,size_t subPos, size_t subLen) { assert(subPos<s.Size()); const char* str=s.Getstr()+subPos; while (subLen) { Insert(pos,*str); ++str; ++subPos; --subLen; ++pos; } } void Erase(size_t pos,size_t len) {    assert(pos<_size);if (pos+len>=_size-1){_size=pos;_str[_size]='\0';}else{char* dst=_str+pos+len;char* src=_str+pos;            strcpy(src,dst);_size-=len;} } int Find(char ch) { size_t i=0; for (i=0;i<_size;i++) { if (ch=_str[i]) { return 1; } else return 0; }      } int Find(const char* sub) { size_t len=strlen(sub); size_t i=0; char* str1=_str;  if (* str1++=*sub++)  {             return 1;  }  else   return 0;     } bool operator<(const String& s) { assert(s._str); } bool operator>(const String& s) { int i = 0; while(_str[i] == s._str[i]) { ++i; } if(_str[i] > s._str[i]) { return true; } return false; }   bool operator>=(const String& s)   {           return   *this==s||*this>s;   }      bool operator<=(const String& s)  {      return !(*this>s);  } bool operator==(const String& s) { int i=0; while(_str[i]==s._str[i]) { ++i; } if (i==_size) { return true; }  return false;  }  bool operator!=(const String& s)  {              return !(*this==s);  }  String SubStr(size_t pos,size_t len)  {  assert(pos<_size);  } void Swap(String& s) { swap(_str, s._str); swap(_size, s._size); swap(_capacity, s._capacity); } private: char* _str; size_t _size; size_t _capacity; } ;
#include <iostream>
using namespace std;
#include <assert.h>
#include <string.h>#include "String.h"
void TestString(){     String s1("hello");   cout<<s1.Getstr()<<endl;  String s2(s1);   s2.Erase(2,2);  cout<<s2.Getstr()<<endl;   s1.Insert(5," world");     cout<<s1.Getstr()<<endl;}int main(){ TestString(); return 0;} 


3 0
原创粉丝点击