1. 下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数。
  2. [cpp] view plain copy
  3. #include<iostream>
  4. #include<iomanip>
  5. using namespace std;
  6. class String{
  7. friend ostream& operator<< (ostream&,String&);//重载<<运算符
  8. friend istream& operator>> (istream&,String&);//重载>>运算符
  9. public:
  10. String(const char* str=NULL); //赋值构造兼默认构造函数(char)
  11. String(const String &other); //拷贝构造函数(String)
  12. String& operator=(const String& other); //operator= //赋值函数
  13. String operator+(const String &other)const; //operator+
  14. bool operator==(const String&); //operator==
  15. char& operator[](unsigned int); //operator[]
  16. size_t size(){return strlen(m_data);};
  17. ~String(void) {delete[] m_data;} //析构
  18. private:
  19. char *m_data; // 用于保存字符串
  20. };
  21. inline String::String(const char* str)
  22. {
  23. if(!str)m_data=0; //声明为inline函数,则该函数在程序中被执行时是语句直接替换,而不是被调用
  24. else {
  25. m_data=new char[strlen(str)+1];
  26. strcpy(m_data,str);
  27. }
  28. }
  29. inline String::String(const String &other)
  30. {
  31. if(!other.m_data)m_data=0;//在类的成员函数内可以访问同种对象的私有成员(同种类则是友元关系)
  32. else
  33. {
  34. m_data=new char[strlen(other.m_data)+1];
  35. strcpy(m_data,other.m_data);
  36. }
  37. }
  38. inline String& String::operator=(const String& other)
  39. {
  40. if (this!=&other)
  41. {
  42. delete[] m_data;
  43. if(!other.m_data) m_data=0;
  44. else
  45. {
  46. m_data = new char[strlen(other.m_data)+1];
  47. strcpy(m_data,other.m_data);
  48. }
  49. }
  50. return *this;
  51. }
  52. inline String String::operator+(const String &other)const
  53. {
  54. String newString;
  55. if(!other.m_data)
  56. newString = *this;
  57. else if(!m_data)
  58. newString = other;
  59. else
  60. {
  61. newString.m_data = new char[strlen(m_data)+strlen(other.m_data)+1];
  62. strcpy(newString.m_data,m_data);
  63. strcat(newString.m_data,other.m_data);
  64. }
  65. return newString;
  66. }
  67. inline bool String::operator==(const String &s)
  68. {
  69. if ( strlen(s.m_data) != strlen(m_data) )
  70. return false;
  71. return strcmp(m_data,s.m_data)?false:true;
  72. }
  73. inline char& String::operator[](unsigned int e)
  74. {
  75. if (e>=0&&e<=strlen(m_data))
  76. return m_data[e];
  77. }
  78. ostream& operator<<(ostream& os,String& str)
  79. {
  80. os << str.m_data;
  81. return os;
  82. }
  83. istream &operator>>( istream &input, String &s )
  84. {
  85. char temp[ 255 ]; //用于存储输入流
  86. input>>setw(255)>>temp;
  87. s = temp; //使用赋值运算符
  88. return input; //使用return可以支持连续使用>>运算符
  89. }
  90. int main()
  91. {
  92. String str1="Aha!";
  93. String str2="My friend";
  94. String str3 = str1+str2;
  95. cout<<str3<<"/n"<<str3.size()<<endl;
  96. return 0;
  97. }


标准库String类的更多相关文章

  1. 实现C++标准库string类的简单版本

    代码如下: #ifndef STRING_H #define STRING_H #include <cassert> #include <utility> #include & ...

  2. C++标准库异常类

    C++标准库异常类 2012-12-24 16:27 5269人阅读 评论(1) 收藏 举报  分类: c/c++(36)  C++标准库异常类继承层次中的根类为exception,其定义在excep ...

  3. C++标准库<string>简单总结

    C++标准库<string>简单总结 在C++中,如果需要对字符串进行处理,那么它自带的标准库<string>无疑是最好的选择,它实现了很多常用的字符处理函数. 要想使用标准C ...

  4. C++异常第二篇---C++标准库异常类exception的使用

    1 继承图示 2 具体讲解 C++标准库异常类继承层次中的根类为exception,其定义在exception头文件中,它是C++标准库所有函数抛出异常的基类,exception的接口定义如下: na ...

  5. 【C++ Primer每日刷】之三 标准库 string 类型

    标准库 string 类型 string 类型支持长度可变的字符串.C++ 标准库将负责管理与存储字符相关的内存,以及提供各种实用的操作.标准库string 类型的目的就是满足对字符串的一般应用. 与 ...

  6. C++标准库string类型

    string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作.标准库string类型的目的就是满足对字符串的一般应用. 本文地址:http://www.cn ...

  7. C++标准库string

    C++标准库string 定义和初始化 string s1 默认初始化,s1是一个空串 string s2(s1) s2是s1的副本 string s2 = s1 等价于s2(s1),s2是s1的副本 ...

  8. c/c++ 标准库 string

    c/c++ 标准库 string 标准库 string的小例子 test1~test10 #include <iostream> using namespace std; int main ...

  9. C 标准库 - string.h

    C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...

随机推荐

  1. ListFragment的使用

    ListFragment继承了Fragment,顾名思义,ListFragment是一种特殊的Fragment,它包含了一个ListView,在ListView里面显示数据. 1. MainActiv ...

  2. ABP Zero示例项目登录报错“Empty or invalid anti forgery header token.”问题解决

    ABP Zero项目,登录时出现如图"Empty or invalid anti forgery header token."错误提示的解决方法: 在 WebModule.cs的P ...

  3. 模拟Bootstrap响应式网格系统

    Bootstrap响应式(适应于不同的终端设备).Bootstrap栅格系统是利用百分比把视口等分为12个,然后利用媒体查询,设置float属性使之并列显示 一.媒体查询 媒体查询包含一个可选的媒体类 ...

  4. UIScrollView的封装

    UIScrollView的封装 效果 特点 1.用法简单,尺寸大小,随意设置位置 2.可以有多个数据源的数据,可以定制不通的界面(如同上图,一个有文字,一个没有文字) 3.能够实现点击事件 用法 1. ...

  5. 【转】漫谈iOS程序的证书和签名机制

    转自:漫谈iOS程序的证书和签名机制 接触iOS开发半年,曾经也被这个主题坑的摸不着头脑,也在淘宝上买过企业证书签名这些服务,有大神都做了一个全自动的发布打包(不过此大神现在不卖企业证书了),甚是羡慕 ...

  6. 强大的flash头像上传插件(支持旋转、拖拽、剪裁、生成缩略图等)

    今天介绍的这款flash上传头像功能非常强大,支持php,asp,jsp,asp.net 调用 头像剪裁,预览组件插件. 本组件需要安装Flash Player后才可使用,请从http://dl.pc ...

  7. 前端开发--ppt展示页面跳转逻辑实现

    1. 工程地址:https://github.com/digitalClass/web_page 网站发布地址: http://115.28.30.25:8029/ 2. 今天遇到一个小问题, 同组的 ...

  8. ERR_CONTENT_DECODING_FAILED错误的原因和解决办法

    1. ERR_CONTENT_DECODING_FAILED错误的原因 这种错误通常发生于Http请求中的头部信息标识内容是gzip编码的,但实际上不是. 2. ERR_CONTENT_DECODIN ...

  9. AspNetPager 多条件分页查询

    AspNetPager 这个分页控件一般做后台基本都知道的,我就不多说了(说明与下载链接:http://www.webdiyer.com/Controls/AspNetPager),嘿嘿!其实我也是刚 ...

  10. 树莓派3B初始化后一些必须的设置

    接上一篇,SSH已经登录成功(http://www.cnblogs.com/crosys/p/6220108.html) 1.树莓派系统的设置 1.1扩展系统空间 因为内存卡还有很多空间没有分配,第一 ...