.    #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. 开源VS扩展CodeMaid介绍

         CodeMaid是一个开源的Visual Studio的扩展插件,用于整理与优化代码等.功能类似于商业软件ReSharper,但它是免费的,并且开放源代码.它能帮助你更容易的理解你的代码,支 ...

  2. js中创建对象的几种方式

    创建对象指创建一个object并给这个对象添加属性和方法,有以下几个方式: 最基本的: var Person={}; Person.name='tom'; Person.age='20'; Perso ...

  3. 初学Node(四)事件循环

    Node中的事件循环 事件循环是Node的核心,正是因为有了事件循环JS才能够在服务端占有一席之地.JS是一种单线程语言,但是它的执行环境是多线程的在加上JS的事件驱动这一特点,使使JS在执行的过程中 ...

  4. DOM性能小记

    在使用DOM操作时,同样的效果用不同的方式来实现,性能方面也会有很大的差异.尤其在移动式设备上,资源本来就很有限,一旦DOM写不好的话操作就会非常卡顿.这个周末,就写个DOM性能小记吧.错漏之处,望多 ...

  5. 0728关于html的几个基础知识点

    1.文本的格式化. 文本的格式包括粗体,斜体等.<b></b>定义粗体,<i>定义斜体</i>,<del></del> 定义删除 ...

  6. Cannot export AX project in AX7

    I tried to export project from VS.  I succeed before.  But today I got a Microsoft Visual Studio err ...

  7. Convert part to feature command

    Search 库主页 Related Links ArcObjects SDK for ArcGIS 10Microsoft Help System Documentation Convert par ...

  8. 安装sql server managerment studio报错"The instance id is required but it is missing"

    问题描述: 今天在安装sql server managerment studio的时候提示报错"The instance id is required but it is missing&q ...

  9. Struts2原理

    Struts 2以WebWork优秀的设计思想为核心,吸收了Struts 1的部分优点,建立了一个兼容WebWork和Struts 1的MVC框架,Struts 2的目标是希望可以让原来使用Strut ...

  10. C99标准的新特性

    C语言标准的发展 C语言的发展历史大致上分为4个阶段:Old Style C.C89.C99和C11. C89是最早的C语言规范,于1989年提出,1990年先由ANSI(美国国家标准委员会,Amer ...