码迷,mamicode.com
首页 > 编程语言 > 详细

mystring c++ 自己的string 封装

时间:2015-06-18 00:41:19      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:


1
/************************************************************************* 2 > File Name: mystring.h 3 > Author: lukey 4 > Mail: lukey123@foxmail.com 5 > Created Time: Wed 17 Jun 2015 08:50:49 PM CST 6 ************************************************************************/ 7 8 #ifndef __MYSTRING__ 9 #define __MYSTRING__ 10 11 class String 12 { 13 public: 14 String(); 15 String(const char *);//有参构造函数 16 String(const String & rhs); //复制构造 17 ~String(); 18 19 String & operator=(const String & rhs);//赋值运算符的两种情况 20 String & operator=(const char *str); 21 22 String & operator+=(const String & rhs); 23 String & operator+=(const char * str); 24 25 char & operator[](std::size_t index); 26 const char & operator[](std::size_t index) const; 27 28 std::size_t size() const; 29 const char* c_str() const; 30 void debug(); 31 32 //String 类和char相加的几个情况 33 friend String operator+(const String & s1, const String & s2); 34 friend String operator+(const String &, const char *); 35 friend String operator+(const char *, const String &); 36 37 friend bool operator==(const String &, const String &); 38 friend bool operator!=(const String &, const String &); 39 40 friend bool operator<(const String &, const String &); 41 friend bool operator>(const String &, const String &); 42 friend bool operator<=(const String &, const String &); 43 friend bool operator>=(const String &, const String &); 44 45 friend std::ostream & operator<<(std::ostream & os, const String &s); 46 friend std::istream & operator>>(std::istream & is, String & s); 47 48 private: 49 char *pstr_; 50 }; 51 52 #endif
  1 /*************************************************************************
  2   > File Name: mystring.cc
  3   > Author: lukey
  4   > Mail: lukey123@foxmail.com 
  5   > Created Time: Wed 17 Jun 2015 09:18:55 PM CST
  6  ************************************************************************/
  7 
  8 #include<iostream>
  9 #include<cstring>
 10 #include<stdlib.h>
 11 #include"mystring.h"
 12 using namespace std;
 13 #if 0
 14 class String
 15 {
 16     public:
 17     private:
 18         char *pstr_;
 19 };
 20 #endif
 21 
 22 //构造函数
 23 String::String()
 24 {
 25     std::cout << "String()" << std::endl;
 26     pstr_  = new char[1];//new 已经初始化了
 27 }
 28 String::String(const char *str)//有参构造函数
 29 {
 30     std::cout << "String(const char * str)" << std::endl;
 31     pstr_ = new char[strlen(str)+1];
 32     strcpy(pstr_, str);
 33 }
 34 String::String(const String & rhs)    //复制构造,考虑自复制情况?
 35 {
 36     std::cout << "String(const String & rhs)" << std::endl;
 37     pstr_ = new char[strlen(rhs.pstr_) + 1];
 38     strcpy(pstr_, rhs.pstr_);
 39 }
 40 String::~String()
 41 {
 42     std::cout << "~String()" << std::endl;
 43     delete []pstr_;
 44 }
 45 
 46 String & String::operator=(const String & rhs)//赋值运算符的两种情况,考虑自赋值情况
 47 {
 48     std::cout << "String & operator=(const String & rhs)" << std::endl;
 49     if(this == &rhs)
 50         return *this;
 51     delete []pstr_;
 52     pstr_ = new char[strlen(rhs.pstr_) + 1];
 53     strcpy(pstr_, rhs.pstr_);
 54     return *this;
 55 }
 56 String & String::operator=(const char *str)
 57 {
 58     std::cout << "String & operator=(const char *str)" << std::endl;
 59     pstr_ = new char[strlen(str) + 1];
 60     strcpy(pstr_, str);
 61     return *this;
 62 }
 63 
 64 String & String::operator+=(const String & rhs)    //rhs连接到pstr_后面
 65 {
 66     std::cout << "operator+=(const String & rhs)" << std::endl;
 67     int len = strlen(rhs.pstr_) + strlen(pstr_);
 68     pstr_ = (char *)realloc(pstr_, len + 1);
 69     strcpy(pstr_, rhs.pstr_);
 70     return *this;
 71 }
 72 String & String::operator+=(const char * str)
 73 {
 74     std::cout << "operator+=(const char * str)" << std::endl;
 75     int len = strlen(str) + strlen(pstr_);
 76     pstr_ = (char *)realloc(pstr_, len + 1);
 77     strcat(pstr_, str);
 78     return *this;
 79 }
 80 
 81 //下标运算符,非常量,可以修改值
 82 char & String::operator[](std::size_t index)
 83 {
 84     return pstr_[index];
 85 }
 86 
 87 //常量对象取下标,不能为其赋值
 88 const char & String::operator[](std::size_t index) const
 89 {
 90     return pstr_[index];
 91 }
 92 
 93 //字符串容量
 94 std::size_t String::size() const
 95 {
 96     return sizeof(pstr_);
 97 }
 98 
 99 //转换成c类型字符串,以‘\0‘结尾
100 const char* String::c_str() const
101 {
102     int len = strlen(pstr_);
103 
104     pstr_[len + 1] = \0;
105     return pstr_;
106 }
107 
108 //不懂?打印出字符串?
109 void String::debug()
110 {
111     std::cout << pstr_ << std::endl;    
112 }
113 
114 String operator+(const String & s1, const String & s2)
115 {
116     std::cout << "operator+(const String & s1,const String & s2)" << std::endl;
117     String ret_str = s1.pstr_;
118     ret_str += s2.pstr_;
119     return ret_str;
120 }
121 
122 String operator+(const String & s, const char * str)
123 {
124     std::cout << "operator+(String, char *)" << std::endl;
125     String temp(str);
126     return (s + temp);    //直接调用上面的(+)函数
127 
128 }
129 
130 String operator+(const char * str, const String & s)
131 {
132     std::cout << "operator+( char *, String)" << std::endl;
133     String temp(str);
134     return (s + temp);    //直接调用上面的(+)函数
135 }
136 
137 bool operator==(const String & lstr, const String & rstr)
138 {
139     std::cout << "==" << std::endl;
140     if(strcmp(lstr.pstr_, rstr.pstr_) == 0)
141         return true;
142     else
143         return false;
144 }
145 
146 bool operator!=(const String & lstr, const String & rstr)
147 {
148     std::cout << "!=" << std::endl;
149     return !(lstr == rstr);
150 }
151 
152 bool operator<(const String & lstr, const String & rstr)
153 {
154     std::cout << "<" << std::endl;
155     if(strcmp(lstr.pstr_, rstr.pstr_) < 0)
156         return true;
157     else
158         return false;
159 }
160 
161 bool operator>(const String & lstr, const String & rstr)
162 {
163     std::cout << ">" << std::endl;
164     if(strcmp(lstr.pstr_, rstr.pstr_) > 0)
165         return true;
166     else
167         return false;
168 }
169 bool operator<=(const String & lstr, const String & rstr)
170 {
171     std::cout << "<=" << std::endl;
172     if(strcmp(lstr.pstr_, rstr.pstr_) <= 0)
173         return true;
174     else
175         return false;
176 }
177 
178 bool operator>=(const String & lstr, const String & rstr)
179 {
180     std::cout << ">=" << std::endl;
181     if(strcmp(lstr.pstr_, rstr.pstr_) >= 0)
182         return true;
183     else
184         return false;
185 }
186 
187 std::ostream & operator<<(std::ostream & os, const String &s)
188 {
189     os << s.pstr_ << " ";
190     return os;
191 }
192 std::istream & operator>>(std::istream & is, String & s)
193 {
194     is >> s.pstr_;
195     return is;        //貌似有坑, 目前不能输入空格
196 }
197 
198 
199 //测试时每个函数调用都打印了信息
200 int main(void)
201 {
202     String s1("hello");
203     s1.debug();
204     std::cout << s1;
205     std::cout << std::endl;
206     String s2("world");
207     s2.debug();
208     if(s1 > s2)
209         std::cout << "s1 > s2" << std::endl;
210 
211     s1 = s2;
212     s1.debug();
213     String s3(s1);
214     s3.debug();
215 
216     String s4 = s2 + s3;
217     s4.debug();
218 
219     String s5;
220     std::cout << s5 << std::endl;
221     std::cin >> s5;
222     std::cout << s5 << std::endl;
223     return 0;
224 }

 

mystring c++ 自己的string 封装

标签:

原文地址:http://www.cnblogs.com/luolizhi/p/4584657.html

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