标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数。[cpp] view plain copy#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=0; //声明为inline函数,则该函数在程序中被执行时是语句直接替换,而不是被调用else {m_data=new char[strlen(str)+1];strcpy(m_data,str);}}inline String::String(const String &other){if(!other.m_data)m_data=0;//在类的成员函数内可以访问同种对象的私有成员(同种类则是友元关系)else{m_data=new char[strlen(other.m_data)+1];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=0;else{m_data = new char[strlen(other.m_data)+1];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)+1];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>=0&&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[ 255 ]; //用于存储输入流input>>setw(255)>>temp;s = temp; //使用赋值运算符return input; //使用return可以支持连续使用>>运算符}int main(){String str1="Aha!";String str2="My friend";String str3 = str1+str2;cout<<str3<<"/n"<<str3.size()<<endl;return 0;}
标准库String类的更多相关文章
- 实现C++标准库string类的简单版本
代码如下: #ifndef STRING_H #define STRING_H #include <cassert> #include <utility> #include & ...
- C++标准库异常类
C++标准库异常类 2012-12-24 16:27 5269人阅读 评论(1) 收藏 举报 分类: c/c++(36) C++标准库异常类继承层次中的根类为exception,其定义在excep ...
- C++标准库<string>简单总结
C++标准库<string>简单总结 在C++中,如果需要对字符串进行处理,那么它自带的标准库<string>无疑是最好的选择,它实现了很多常用的字符处理函数. 要想使用标准C ...
- C++异常第二篇---C++标准库异常类exception的使用
1 继承图示 2 具体讲解 C++标准库异常类继承层次中的根类为exception,其定义在exception头文件中,它是C++标准库所有函数抛出异常的基类,exception的接口定义如下: na ...
- 【C++ Primer每日刷】之三 标准库 string 类型
标准库 string 类型 string 类型支持长度可变的字符串.C++ 标准库将负责管理与存储字符相关的内存,以及提供各种实用的操作.标准库string 类型的目的就是满足对字符串的一般应用. 与 ...
- C++标准库string类型
string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作.标准库string类型的目的就是满足对字符串的一般应用. 本文地址:http://www.cn ...
- C++标准库string
C++标准库string 定义和初始化 string s1 默认初始化,s1是一个空串 string s2(s1) s2是s1的副本 string s2 = s1 等价于s2(s1),s2是s1的副本 ...
- c/c++ 标准库 string
c/c++ 标准库 string 标准库 string的小例子 test1~test10 #include <iostream> using namespace std; int main ...
- C 标准库 - string.h
C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...
随机推荐
- Yii 2.x RESTful 应用 - 类图
配置url管理器配置请求数据解析器配置用户控制器 ['GET', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']
- Java终止线程
Thread提供了stop()方法终止线程,但是该方法是强行终止,容易产生一些错误,已经被废弃. 可以使用退出标志来终止线程,在run()函数里面设置while循环,把退出标志作为while的条件,当 ...
- 偷天换日:网络劫持,网页js被伪装替换。
偷天换日 3月12号石家庄一个客户(后面简称乙方)有几家门店,平台收银(web)有一些功能无法正常使用,平台有上千家门店在使用,到目前为止别的省份都没有此问题.远程协助发现,js日期控件无法正常调用, ...
- 【转】zigbee终端无法重连的问题解决
zigbee终端无法重连的问题解决 1.zigbee重连的原因 (1)zigbee由于各种原因的干扰导致信号太差而掉线. (2)协调器重启. 2.zigbee终端重连的处理 (1)zigbee掉线后会 ...
- pandas.DataFrame排除特定行
使用Python进行数据分析时,经常要使用到的一个数据结构就是pandas的DataFrame 如果我们想要像Excel的筛选那样,只要其中的一行或某几行,可以使用isin()方法,将需要的行的值以列 ...
- 《连载 | 物联网框架ServerSuperIO教程》1.4种通讯模式机制。附小文:招.NET开发,结果他转JAVA了,一切都是为了生活
参考文章: 1.SuperIO通讯框架介绍,含通信本质 2.C#跨平台物联网通讯框架ServerSuperIO(SSIO) 一.感慨 上大学的时候,没有学过C#,花了5块钱在地坛书市买了一本教程,也就 ...
- Eclipse 日期和时间格式自定义
点击下载Eclipse插件 org.eclipse.text_3.5.300.v20130515-1451.jar 覆盖下图所示的jar文件. /************************* ...
- react动画难写?试试react版transformjs
简介 transformjs在非react领域用得风生水起,那么react技术栈的同学能用上吗?答案是可以的.junexie童鞋已经造了个react版本. 动画实现方式 传统 web 动画的两种方式: ...
- iOS开发常用快捷键
二. Xcode基本快捷键 新建项目 Shift + CMD + N 项目中新建文件 CMD + N 运行 CMD + R 编译 CMD + B 停止运行 CMD + . 清除缓存 Shift + C ...
- the request resource is not available
form表单递交数据的问题 我的解决方法 将要访问的servlet地址写入form的action中 例如:访问地址为http://localhost:8080/Webprojectselfservic ...