C++实现string类

C++面试或者笔试的时候经常遇到这样一个问题,就是自己实现一个string类。

本人总结自己的面试经验以及参考网上各位网友的总结,总得来说常见的主要实现的包括以下几个方面(如有不如,欢迎补充)

常见:普通的构造函数、拷贝构造函数、析构函数、字符串的链接即‘+’号运算符重载、字符串赋值即‘=’号运算符重载、字符串是否相等即‘==’号运算符重载、获取字符串长度

不常见:输入‘>>’重载、输出‘<<'重载

StringRealiza.h

复制代码
 1 #pragma once
 2 
 3 #include <iostream>
 4 
 5 class StringRealize
 6 {
 7     friend std::ostream& operator<<(std::ostream& os, const StringRealize& str);//输出符重载
 8     friend std::ostream& operator>>(std::ostream& os, const StringRealize& str);//输入符重载
 9 
10 public:
11     StringRealize(const char *str = NULL);//普通构造函数
12     StringRealize(const StringRealize& str);//拷贝构造函数
13     ~StringRealize(void);//析构函数
14 
15     StringRealize& operator=(const StringRealize& str);//赋值运算符'='号重载
16     StringRealize operator+(const StringRealize& str);//链接字符串‘+’号重载
17     bool operator==(const StringRealize& str);//等号运算符‘==’号重载
18 
19     int getLength(void);//获取字符串的长度
20 private:
21     char *m_data;//用于保存字符串
22 };
复制代码

StringRealiza.cpp

复制代码
  1 #include "StringRealize.h"
  2 
  3 //普通构造函数
  4 StringRealize::StringRealize(const char *str)
  5 {
  6     if (str == NULL)
  7     {
  8         this->m_data = NULL;
  9     }
 10     else
 11     {
 12         this->m_data = new char[strlen(str) + 1];
 13         strcpy(this->m_data,str);
 14     }
 15 }
 16 
 17 //拷贝构造函数
 18 StringRealize::StringRealize(const StringRealize& str)
 19 {
 20     if (str.m_data == NULL)
 21     {
 22         this->m_data = NULL;
 23     }
 24     else
 25     {
 26         this->m_data = new char[strlen(str.m_data) + 1];
 27         strcpy(this->m_data,str.m_data);
 28     }
 29 }
 30 
 31 //析构函数
 32 StringRealize::~StringRealize(void)
 33 {
 34     if (this->m_data)
 35     {
 36         delete[] this->m_data;
 37         this->m_data = NULL;
 38     }
 39 }
 40 
 41 //赋值运算符'='号重载
 42 StringRealize& StringRealize::operator=(const StringRealize& str)
 43 {
 44     if (this != &str)
 45     {
 46         delete[] this->m_data;
 47         this->m_data = NULL;
 48 
 49         if (str.m_data == NULL)
 50         {
 51             this->m_data = NULL;
 52         }
 53         else
 54         {
 55             this->m_data = new char[strlen(str.m_data) + 1];
 56             strcpy(this->m_data,str.m_data);
 57         }
 58     }
 59 
 60     return *this;
 61 }
 62 
 63 //链接字符串‘+’号重载
 64 StringRealize StringRealize::operator+(const StringRealize& str)
 65 {
 66     StringRealize newString;
 67     if (str.m_data == NULL)
 68     {
 69         return *this;
 70     } 
 71     else if (this->m_data == NULL)
 72     {
 73         newString = str;
 74     }
 75     else
 76     {
 77         newString.m_data = new char[strlen(str.m_data) + strlen(this->m_data) + 1];
 78         strcpy(newString.m_data,this->m_data);
 79         strcpy(newString.m_data,str.m_data);
 80     }
 81 
 82     return newString;
 83 }
 84 
 85 //等号运算符‘==’号重载
 86 bool StringRealize::operator==(const StringRealize& str)
 87 {
 88     if (strlen(this->m_data) != strlen(str.m_data)  )
 89     {
 90         return false;
 91     }
 92     else
 93     {
 94         return strcmp( this->m_data, str.m_data ) ? false : true;
 95     }
 96 
 97 }
 98 
 99 //获取字符串的长度
100 int StringRealize::getLength()
101 {
102     return strlen(this->m_data);
103 }
104 
105 //输出符重载
106 std::ostream& operator<<(std::ostream& os, const StringRealize& str)
107 {
108     os<<str.m_data;
109     return os;
110 }
111 
112 //输入符重载
113 std::ostream& operator>>(std::ostream& os, const StringRealize& str)
114 {
115     os>>str.m_data;
116     return os;
117 }
复制代码
posted @ 2019-05-19 22:27  unique_ptr  阅读(584)  评论(0编辑  收藏  举报