c++string 类的简单实现(增删查改功能)

能够准确无误地编写出String类的构造函数、拷贝构造函数、赋值函数和析构函
数的面试者至少已经具备了C++基本功的60%以上!在这个类中包括了指针类成员变量m_data,当类中包括指针类成员变量时,一定要重载其拷贝构造函数、赋值函数和析构函数,这既是对C++程序员的基本要求,也是《Effective C++》中特别强调的条款。仔细学习这个类,特别注意加注释的得分点和加分点的意义,这样就具备了60%以上的C++基本功!——百度百科

由此可见string的必要性和重要性

#include<assert.h>
#include<string.h>
#include<iostream>
using namespace std;

class String 
{ 
public: 
  String( char* str = NULL){
    _size=strlen(str);
    _capacity=_size;
    _str=new char[strlen(str)+1];
    strcpy(_str,str);
  }//通用构造函数 
  String(const String& s){          //深拷贝
    _str=new char[strlen(s._str)+1];

    strcpy(_str,s._str);
    _capacity=_size;
    _size=strlen(s._str);
  } //拷贝构造   也可以使用写时拷贝[这里写链接内容](https://blog.csdn.net/important_/article/details/75439911)
  String& operator=(String s){
    Swap(s);
    return *this;
  }
void Swap(String& s){
  swap(_str,s._str);
  _capacity=s._capacity;
  _size=s._size;
} 
~String(){
  if(_str){
    delete [] _str;
    _str=NULL;
  }
  _size=0;
  _capacity=0;
} 
char* GetStr(){
  return _str;
}
size_t Size(){

  return _size;
} 
size_t Capacity(){
  return _capacity;
} 

// 增删查改 
void PushBack(char ch){//尾部插入字符串
  if(_size==_capacity){
    Expand(2*_capacity+1);
  }
  _str[_size]=ch;
  ++_size;
  _str[_size]='\0';
} 
void PushBack(const char* str) { //插入字符数组
  int len =strlen(str);
  if(_size+len>_capacity){
    Expand(_size+len+1);
  }
  for(int i=0;i<len;i++){
    _str[i+_size]=str[i];
  }
  _size=_size+len;
  _str[_size]='\0';
  _capacity=_size;


}
void PopBack(){            // 尾删

  if(_size){
    _str[_size-1]='\0';
    --_size;

}
}
void Insert(size_t pos, char ch){ //pos前插入一个字符

  if(_size==_capacity){
    Expand(2*_capacity+1);
  }
  int end=_size;
  //挪动数据
  while(end>=(int)pos){
   _str[end+1]=_str[end];
   --end;
  }
  _str[pos]=ch;
  ++_size;
}
void Insert(size_t pos, const char* str){
  size_t len =strlen(str);
  if(_size==_capacity){
    Expand(2*_capacity+1);
  }
  int end=_size;
  //挪动数据
  while(end>=(int)pos){
    _str[end+1]=_str[end];
    --end;
  }
  while(*str){
    _str[pos++]=*str++;
    _size=_size+len;
  }
}
void Erase(size_t pos, size_t count){ //从指定节点开始删除count长度

  if(pos+count>=_size-1){
    _str[pos]='\0';
    _size=pos;
  }else{
    strcpy(_str+pos,_str+pos+count);
    _size -=count;
  }
}
size_t Find(char ch) const{

  for(int i=0;i<(int)_size;i++){
    if(_str[i]==ch){
      return i;
    }
  }
  return -1;
} 
size_t Find(const char* str) const{
  assert(str);
  const char* str1=_str;
  const char* str2= str;
  size_t len2=strlen(str);
  size_t a1=0,a2=0;
  while(len2<_size){
    while(str1[a1]==str2[a2]){
      ++a1;
      ++a2;
      if(len2==a2){
        return a2;
      }
    }
    a2=0;
    a1++;
  }
  return -1;
} 
char& operator[](size_t pos)
{
  assert(pos<_size);
  return _str[pos];
}
bool operator<(const String& s) const{
  size_t i=0;
  for(;i<_size&&i<s._size;i++){
    if(_str[i]<s._str[i]){
      return true;
    }else if(_str[i]>s._str[i]){
      return false;
    } 
  }
  if(i==_size&&i<s._size){
    return true;
  }else{
    return false;
  }
} 
bool operator<=(const String& s) const{
  return *this<s||*this==s;
} 
bool operator>(const String& s) const{
  return !(*this<=s);
}

bool operator>=(const String& s) const{
  return !(*this<s);
} 
bool operator==(const String& s) const{
  size_t i=0;
  for(;i<_size&&i<s._size;i++){
    if(_str[i]!=s._str[i]){
      return false;
    }
  }
  if(i==_size&&i==s._size){
    return true;
  }else{
    return false;
  }
} 
bool operator!=(const String& s)const{

  return !(*this==s);

} 
void Expand(size_t n){ //增容
  char * tmp=new char[n];
  strcpy(tmp,_str);
  _str=tmp;
  _capacity=n-1;
} 
private: 
      char * _str; 
      size_t _size; // 字符个数 
      size_t _capacity; // 容量空间 
};

int main(){



  String s1("hello");
  //s1.push_back('');
  //s1.push_back('world');
  //String s2(s1);
  //cout<<s1.GetStr()<<endl;
  //cout<<s2.GetStr()<<endl;
  s1.PushBack(' ');
    s1.PushBack('w');
    s1.PushBack('o');
    s1.PushBack('r');
    s1.PushBack('l');
    s1.PushBack('d');
    cout<<s1.GetStr()<<endl;
    //cout<<s2.GetStr()<<endl;
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值