string 类的实现
. #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 类的实现的更多相关文章
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- 自己实现简单的string类
1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...
- C++ string类的实现
c++中string类的实现 今天面试被考到了, 全给忘记了!!! //string类的实现 #include <iostream> #include <string.h> ...
- String类的功能
String类 标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...
- java基础复习:final,static,以及String类
2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...
- String类和StringBuffer类的区别
首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...
- 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明
Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- String类常用方法
1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
随机推荐
- nginx跨域处理
http://www.nginx.cn/nginx-download nginx.conf配置 if ($request_method = ‘OPTIONS’) { add_heade ...
- 「C语言」C输出hello world!系统发生了什么?
本篇文章全部摘抄自学长博客供以后学习: http://efraim.me/2015/12/05/tech-linux-2015-12-05/ 排版因与博客园编辑器不同而稍作修改. 输出hello wo ...
- Android笔记——Android中数据的存储方式(一)
Android中数据的存储方式 对于开发平台来讲,如果对数据的存储有良好的支持,那么对应用程序的开发将会有很大的促进作用. 总体的来讲,数据存储方式有三种:一个是文件,一个是数据库,另一个则是网络.其 ...
- C++调用C#dll类库中的方法(非显性COM)
一般在网上搜C++如何调用C#的函数,出来的结果都是做成COM组件,但是这种方法dll安装麻烦,需要注册COM组件,需要管理员权限,调试麻烦,经常需要重启机器,反正有诸多不便. 然后在看<CLR ...
- ArcGIS使用Python脚本工具
在Pyhton写的一些代码,用户交互不方便,用户体验比较差,不方便重用.在ArcGIS中可以将用写的Python代码导入到ToolBox中,这样用起来就比较方便了.这里用按要素裁剪栅格的Python来 ...
- sharepoint获取exchange邮箱报错:该帐户无权模拟所请求的用户
现象: sharepoint获取exchange邮箱报错:该帐户无权模拟所请求的用户 处理办法: 1.Open the Exchange Management Shell 2.输入: New-Mana ...
- ogrinfo使用
简介 orginfo是OGR模块中提供的一个重要工具,用于读取地图文件中记录,可以指定筛选条件(按字段.sql.矩形范围) 使用方式 命令行参数 Usage: ogrinfo [--help-gene ...
- UIScrollView循环滚动1
现在基本每一个商业APP都会有循环滚动视图,放一些轮播广告之类的,都是放在UIScrollView之上.假如我要实现N张图片的轮播,我借鉴了几个博文,得到两种方法实现: [第一种]:如下图(图片来源于 ...
- Reveal使用步骤和 破解Revealapp的试用时间限制
下载地址:http://pan.baidu.com/s/1eQstR2M 一.Reveal使用步骤 1.启动Reveal --> Help --> Show Reveal Library ...
- Java Concurrency In Practice -Chapter 2 Thread Safety
Writing thread-safe code is managing access to state and in particular to shared, mutable state. Obj ...