.    #include<iostream>
. #include<iomanip>
. using namespace std;
.
. class String{
. friend ostream& operator<< (ostream&,String&);//重载<<运算符
. friend istream& operator>> (istream&,String&);//重载>>运算符
. public:
. String(const char* str=NULL); //赋值构造兼默认构造函数(char)
. String(const String &other); //赋值构造函数(String)
. String& operator=(const String& other); //operator=
. String operator+(const String &other)const; //operator+
. bool operator==(const String&); //operator==
. char& operator[](unsigned int); //operator[]
. size_t size(){return strlen(m_data);};
. ~String(void) {delete[] m_data;}
. private:
. char *m_data; // 用于保存字符串
. };
.
. inline String::String(const char* str)
. {
. if(!str)m_data=; //声明为inline函数,则该函数在程序中被执行时是语句直接替换,而不是被调用
. else {
. m_data=new char[strlen(str)+];
. strcpy(m_data,str);
. }
. }
.
. inline String::String(const String &other)
. {
. if(!other.m_data)m_data=;//在类的成员函数内可以访问同种对象的私有成员(同种类则是友元关系)
. else
. {
. m_data=new char[strlen(other.m_data)+];
. strcpy(m_data,other.m_data);
. }
. }
.
. inline String& String::operator=(const String& other)
. {
. if (this!=&other)
. {
. delete[] m_data;
. if(!other.m_data) m_data=;
. else
. {
. m_data = new char[strlen(other.m_data)+];
. strcpy(m_data,other.m_data);
. }
. }
. return *this;
. }
. inline String String::operator+(const String &other)const
. {
. String newString;
. if(!other.m_data)
. newString = *this;
. else if(!m_data)
. newString = other;
. else
. {
. newString.m_data = new char[strlen(m_data)+strlen(other.m_data)+];
. strcpy(newString.m_data,m_data);
. strcat(newString.m_data,other.m_data);
. }
. return newString;
. }
.
. inline bool String::operator==(const String &s)
. {
. if ( strlen(s.m_data) != strlen(m_data) )
. return false;
. return strcmp(m_data,s.m_data)?false:true;
. }
.
. inline char& String::operator[](unsigned int e)
. {
. if (e>=&&e<=strlen(m_data))
. return m_data[e];
. }
.
. ostream& operator<<(ostream& os,String& str)
. {
. os << str.m_data;
. return os;
. }
.
. istream &operator>>( istream &input, String &s )
. {
. char temp[ ]; //用于存储输入流
. input>>setw()>>temp;
. s = temp; //使用赋值运算符
. return input; //使用return可以支持连续使用>>运算符
. }

string 类的实现的更多相关文章

  1. 标准库String类

    下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...

  2. 自己实现简单的string类

    1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...

  3. C++ string类的实现

    c++中string类的实现 今天面试被考到了, 全给忘记了!!!   //string类的实现 #include <iostream> #include <string.h> ...

  4. String类的功能

    String类              标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...

  5. java基础复习:final,static,以及String类

    2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...

  6. String类和StringBuffer类的区别

    首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...

  7. 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明

    Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...

  8. 标准C++中的string类的用法总结

    标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...

  9. String类常用方法

    1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...

  10. 运用String类实现一个模拟用户登录程序

    package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...

随机推荐

  1. XLConnect:一个用R处理Excel文件的高效平台

    code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...

  2. Android笔记——Windows环境下Android Studio v1.0安装教程

    本文主要讲解Windows环境下Android Studio的安装教程,Mac的Android Studio安装与此类似不在赘述,另外友情提示Windows下的SDK与Mac的SDK是通用的,可以直接 ...

  3. 【GOF23设计模式】适配器模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_适配器模式.对象适配器.类适配器.开发中场景  适配器模式  笔记本电脑只有USB接口,新买的键盘是PS2接口的,需要用适 ...

  4. html,xhtml和xml

    html,xhtml和xml的定义: 1.html即是超文本标记语言(Hyper Text Markup Language),是最早写网页的语言,但是由于时间早,规范不是很好,大小写混写且编码不规范: ...

  5. jquery实现页面控件拖动效果js代码

    ;(function($) { var DragPanelId = "divContext"; var _idiffx = 0; var _idiffy = 0; var _Div ...

  6. ASP.NET获取请求的url信息汇总

    ASP.NET获取请求的url信息汇总 最近做项目需要处理一个用代码获取当前网站的域名或ip信息的问题,于是尝试了ASP.NET中各种获取url信息的方法,在此总结一下: 在Global.asax文件 ...

  7. How-to: disable the web-security-check in Chrome for Mac

    When I try to test one web app in coperate intranet, there is always some error like "Failed to ...

  8. oracle断电重启之ORA-01033和ORA-01172

    参考文献: ORA-01033:解决方法 数据库掉电后 ORA-01172 磁盘坏块解决方法 --尝试连接数据库prjdb C:\Documents and Settings\Administrato ...

  9. Android 带清除功能的输入框控件EditText

    1.效果图      2.源码下载 http://download.csdn.net/detail/yanzi2015/8864603 3.相关博客 http://www.cnblogs.com/to ...

  10. Android Studio 插件的使用

    1.GsonFormat https://github.com/zzz40500/GsonFormat 2.Android SelectorChapek     http://blog.csdn.ne ...