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. Java的内存分配

    java内存分配 A:栈 存储局部变量 B:堆 存储所有new出来的 C:方法区(方法区的内存中) 类加载时 方法信息保存在一块称为方法区的内存中, 并不随你创建对象而随对象保存于堆中; D:本地方法 ...

  2. 转载:TypeScript 简介与《TypeScript 中文入门教程》

    简介 TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程.安德斯·海尔斯伯格,C#的首席架构 ...

  3. 应用SqlGeometry无法加载sqlserverspatial.dll

    最近需要完成一个API,通过用户上传的经纬度判断用户的所在县市省,数据量相对不是很大所以把相关数据全部扔到了内存里知行,主要用到了SqlGeometry, 代码写完后运行本地没问题,扔到服务器上开始报 ...

  4. php注释规范

    注释在写代码的过程中非常重要,好的注释能让你的代码读起来更轻松,在写代码的时候一定要注意注释的规范.(李昌辉) php里面常见的几种注释方式: 1.文件头的注释,介绍文件名,功能以及作者版本号等信息 ...

  5. 一分钟搞定AlloyTouch图片轮播组件

    轮播图也涉及到触摸和触摸反馈,同时,AlloyTouch可以把惯性运动打开或者关闭,并且设置min和max为运动区域,超出会自动回弹. 除了一般的竖向滚动,AlloyTouch也可以支持横向滚动,甚至 ...

  6. jQuery实现DOM加载方法源码分析

    传统的判断dom加载的方法 使用 dom0级 onload事件来进行触发所有浏览器都支持在最初是很流行的写法 我们都熟悉这种写法: window.onload=function(){ ... }  但 ...

  7. 纯js实现10分钟倒计时

    一个简单实现倒计时的小栗子~ 效果图:简陋的不能再简陋了,捂脸 代码: <!DOCTYPE HTML> <html> <head> <title> 倒计 ...

  8. SQL Server 进阶 01 数据库的设计

    SQL Server 进阶 01 数据库的设计 本篇目录 课程内容回顾及介绍 为什么需要规范的数据库设计 设计数据库的步骤 绘制E-R(实体-关系)图 实体-关系模型 如何将E-R图转换为表 数据规范 ...

  9. DBCC CHECKDB 遭遇Operating system error 112(failed to retrieve text for this error. Reason: 15105) encountered

    我们一个SQL Server服务器在执行YourSQLDBa的作业YourSQLDba_FullBackups_And_Maintenance时遇到了错误: Exec YourSQLDba.Maint ...

  10. DependencyResolver.Current

    描述: 获取依赖关系解析程序的实现. 命名空间: System.Web.Mvc 程序集: System.Web.Mvc(在 System.Web.Mvc.dll 中) 用例: IRecLockServ ...