码迷,mamicode.com
首页 > 其他好文 > 详细

string类的两种实现方法及string的一些成员函数的实现

时间:2016-03-12 14:53:44      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:string类实现 成员函数 insert

string的第一种实现方法:

#include<iostream>
using namespace std;
class String
{
public:
     String(char *str="")//构造函数
      :_str(new char[strlen(str)+1])
     {
          strcpy(_str, str);
     }
     String(const String& str)//拷贝构造函数,实现深拷贝
     :_str(new char[strlen(str._str) + 1])
     { 
          strcpy(_str, str._str);
     }
      String& operator=(const String& str)//赋值操作符重载
     {
          if (this != &str)//判断是否是自赋值
          {
               delete[] _str;//释放原来得空间
               _str = new char[strlen(str._str) + 1];//开辟新的空间
               strcpy(_str, str._str);//赋值
           }
         return *this;//返回
     }
     ~String()//析构函数
     {
          delete[] _str;
          _str = NULL;
     }
private:
     char *_str;
};

string的第二种实现方法及一些成员函数的实现:

#include<iostream>
using namespace std;
#define DEFAULT_CAPACITY 3//默认容量
class String
{
public:
     String(char *str = "")//构造函数
      :_str(new char[strlen(str) + 1 + DEFAULT_CAPACITY])
      , _size(strlen(str))
      , _capacity(_size +1+DEFAULT_CAPACITY)
     {
          strcpy(_str, str);
     }
     ~String()//析构函数
     {
          if (_str != NULL)
          {
           delete[] _str;
           _str = NULL;
   
          }
     }
     String(const String& str)//拷贝构造函数
      :_str(NULL)
      , _size(str._size)
      , _capacity(str._capacity)
     {
          String tmp(str._str);//建立临时变量,tmp中的内容与str中的一样
          swap(_str, tmp._str);//交换两个字符串的地址,tmp出了函数系统自动调用析构函数回收内存空间
     }
     String& operator=(String str)//赋值运算符重载,参数是用拷贝构造函数构造的临时变量
     {
          _size = str._size;
          _capacity = str._capacity;
          swap(_str, str._str);//与拷贝构造函数的实现方法一致
          return *this;
      }
     bool operator==(const String& str);
     bool operator>(const String& str);
     bool operator<(const String& str);
     void CheckCapacity(int size);//检查容量是否够用,若是不够则扩容
     void insert(int pos, char ch);//按位置插入一个字符
     void insert(int pos, char *str);//按位置插入字符串
     void insert(int pos, const String& str);//按位置插入一个string类的字符串
     void PushBack(int ch);//尾插
     friend ostream& operator<<(ostream& output, const String& str);
     int MyStrcmp(const char *str1, const char *str2)
private:
     char *_str;
     int _size;//字符个数
     int _capacity;//容量
};

void String::PushBack(int ch)
{
     insert(_size, ch);
}
int String::MyStrcmp(const char *str1, const char *str2)
{
     while (*str1 == *str2)
     {
          if (*str1 == ‘\0‘)
           return 0;
          str1++;
          str2++;
     }
     return *str1 - *str2;
}
bool String::operator==(const String& str)
{
     if (MyStrcmp(_str, str._str) == 0)
          return true;
     else
          return false;
}
bool String::operator>(const String& str)
{
     if (MyStrcmp(_str, str._str) > 0)
          return true;
     else
          return false;
}
bool String::operator<(const String& str)
{
     if (MyStrcmp(_str, str._str) > 0)
          return true;
     else
          return false;
}
void String::CheckCapacity(int size)
{
     if (_capacity < size)
     {
          char *str = new char[size + DEFAULT_CAPACITY];
          strcpy(str, _str);
          delete[] _str;
          _str = str;
          _capacity = size + DEFAULT_CAPACITY;
     }
}
void String::insert(int pos, char ch)
{
     CheckCapacity(_size + 2);
     int end = _size;
     while (end >= pos)
     {
          _str[end + 1] = _str[end];
          end--;
     }
     _str[pos] = ch;
     _size += 1;
}
void String::insert(int pos, char *str)
{
     int len = strlen(str);
     CheckCapacity(_size + len+1);
     int end = _size;
     while (end >= pos)
     {
          _str[end +len] = _str[end];
          end--;
     }
     while (*str)
     {
          _str[pos++] = *str++;
     }
     _size = _size + len;
}
void String::insert(int pos, const String& str)
{
     insert(pos, str._str);
}
ostream& operator<<(ostream& output, const String& str)
{
     output << str._str;
     return output;
}

string类的两种实现方法及string的一些成员函数的实现

标签:string类实现 成员函数 insert

原文地址:http://haipi.blog.51cto.com/10778780/1750201

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!