string的简单实现

简介: string的简单实现

创造一个string的对象,含有char*str和一下基本成员函数

class String
{ //友原重载<<和>>
  friend ostream& operator<<(ostream& out, const String& s);
  friend istream& operator>>(istream& in, String& s);
public:
  String() {
    str = new char[100];
    str[0] = '\0';
  }//string的默认构造
  String(const char* other)
  {
    str = new char[strlen(other) + 1];
    strcpy(str, other);
  }//有参构造
  ~String()
  {
    delete[]str;
  }//析构
  String(const String& other);//复制构造
  int Length();//字符串的长度
  String& operator=(const String& other);
  String& operator=(const char* ch);//重载=
  String operator+(const String& other);//重载+
  String& operator+=(const String& other);//重载+=
  char& operator[](int x);//重载[]
  bool operator==(const String& other);//重载==
  bool operator<(const String& other);//重载<
private:
  char* str;
};

下面逐步写出每个成员函数

friend ostream& operator<<(ostream& out, const String& s);

friend istream& operator>>(istream& in, String& s);

ostream& operator<<(ostream& out, const String& s)
{
  out << s.str;
  return out;
}
istream& operator>>(istream& in, String& s)
{
  char ch[1000];//临时创造一个数组储存输入值
  in >> ch;
  delete[]s.str;//释放s的空间
  s.str = new char[strlen(ch) + 1];//为s重新分配新的空间
  strcpy(s.str, ch);//赋值给s
  return in;
}

String(const String& other);

String::String(const String& other)
{
  str = new char[strlen(other.str) + 1];
  strcpy(str, other.str);
}

int Length();

int String::Length()
{
  return strlen(str);//返回str的长度
}

String& operator=(const String& other);

String& operator=(const char* ch);

String& operator=(const String& other)
  {
    if (this != &other)//判断是否和原来一样,一样直接返回原值
    {
      delete[]str;
      str = new char[strlen(other.str) + 1];
      strcpy(str, other.str);
    }
    return *this;
  }//重载与string的=
  String& operator=(const char* ch)
  {
    delete[]str;
    str = new char[strlen(ch) + 1];
    strcpy(str, ch);
    return *this;
  }//重载与字符串的=

String operator+(const String& other);

String operator+(const String& other)
  {
    int len = strlen(str) + strlen(other.str) + 1;
    //求出需要开辟空间的大小
    char* newstr = new char[len];//用一个新指针去接收
    strcpy(newstr, str);
    strcat(newstr, other.str);
    return String(newstr);
  }//重载+

String& operator+=(const String& other);

String& operator+=(const String& other)
  {
    *this = *this + other;
    return *this;
  }//使用重载后的=和+完成+=的重载

char& operator[](int x);

bool operator==(const String& other);

bool operator<(const String& other);

  char& operator[](int x)
  {
    assert(x >= 0 && x <= strlen(str));
    //使用断言判断是否在范围内
    return str[x];
  }//重载[]
  bool operator==(const String& other)
  {
    return !strcmp(str, other.str);
  }//重载==
  bool operator<(const String& other)
  {
    return strcmp(str, other.str) < 0;
  }//重载<

完整代码及样例的使用

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
class String
{ //友原重载<<和>>
  friend ostream& operator<<(ostream& out, const String& s);
  friend istream& operator>>(istream& in, String& s);
public:
  String() {
    str = new char[100];
    str[0] = '\0';
  }//string的默认构造
  String(const char* other)
  {
    str = new char[strlen(other) + 1];
    strcpy(str, other);
  }//有参构造
  ~String()
  {
    delete[]str;
  }//析构
  String(const String& other);//复制构造
  int Length();//字符串的长度
  String& operator=(const String& other)
  {
    if (this != &other)
    {
      delete[]str;
      str = new char[strlen(other.str) + 1];
      strcpy(str, other.str);
    }
    return *this;
  }//重载与string的=
  String& operator=(const char* ch)
  {
    delete[]str;
    str = new char[strlen(ch) + 1];
    strcpy(str, ch);
    return *this;
  }//重载与字符串的=
  String operator+(const String& other)
  {
    int len = strlen(str) + strlen(other.str) + 1;
    char* newstr = new char[len];
    strcpy(newstr, str);
    strcat(newstr, other.str);
    return String(newstr);
  }//重载+
  String& operator+=(const String& other)
  {
    *this = *this + other;
    return *this;
  }//使用重载后的=和+完成+=的重载
  char& operator[](int x)
  {
    assert(x >= 0 && x <= strlen(str));
    return str[x];
  }//重载[]
  bool operator==(const String& other)
  {
    return !strcmp(str, other.str);
  }//重载==
  bool operator<(const String& other)
  {
    return strcmp(str, other.str) < 0;
  }//重载<
private:
  char* str;
};
String::String(const String& other)
{
  str = new char[strlen(other.str) + 1];
  strcpy(str, other.str);
}
ostream& operator<<(ostream& out, const String& s)
{
  out << s.str;
  return out;
}
istream& operator>>(istream& in, String& s)
{
  char ch[1000];
  in >> ch;
  delete[]s.str;
  s.str = new char[strlen(ch) + 1];
  strcpy(s.str, ch);
  return in;
}
int String::Length()
{
  return strlen(str);
}
int main()
{
  String s1("Help!"), s2("Good!"), s3(s2), s4, s5;
  cout << "s1=" << s1 << endl;
  s3 = "Hello!";
  cout << "s3=" << s3 << endl;
  s3 = s2;
  cout << "s3=" << s3 << endl;
  s3 += s2;
  cout << "s3=" << s3 << endl;
  cin >> s4;
  cout << "s4=" << s4 << endl;
  s5 = s3 + s4;
  cout << "s5=" << s5 << endl;
  s5[0] = 'g';
  cout << "s5=" << s5 << endl;
  cout << "strlen(s5)=" << s5.Length() << endl;
  cout << boolalpha << (s3 == s1) << endl;
  //利用boolalpha来断真假
  cout << boolalpha << (s3 < s1) << endl;
}

运行结构

目录
相关文章
每日一道面试题之String常用的方法有哪些?
每日一道面试题之String常用的方法有哪些?
|
9月前
|
存储 C++ 索引
string的几种常见使用方法
string的几种常见使用方法
136 0
|
9月前
|
存储 算法 C++
【C++】:string用法详解
【C++】:string用法详解
152 0
|
Java C#
C# String与StringBuilder的区别和使用方法
C# String与StringBuilder的区别和使用方法
|
C语言 C++
C++ string 基本用法
C++ string 基本用法
|
编译器 容器
【string 类的使用方法(总结)】
【string 类的使用方法(总结)】
120 0
|
索引
关于String的常用方法
关于String的常用方法
87 0
|
C语言 C++
C++中string的用法
⭐一、string的简介 string的中文译为字符串,是C++在C语言中的char字符类型上的延伸。C++中封装有许多关于string的函数可以方便我们完成一些关于字符串的操作。而且string的空间大小是动态变化的,可以减小不必要空间的浪费。
231 0
|
存储 索引
String 类的基本用法及String 类的常见操作(一)
String 类的基本用法及String 类的常见操作
String 类的基本用法及String 类的常见操作(一)
|
存储 缓存 Java
String源码分析
首先,将一个类分为几个部分,分别是类定义(继承,实现接口等),全局变量,方法,内部类等等,再分别对这几个部分进行说明,这样到最后类的全貌也就比较直观了
192 0