标准库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 ...
随机推荐
- C#开发微信门户及应用(33)--微信现金红包的封装及使用
我在上篇随笔<C#开发微信门户及应用(32)--微信支付接入和API封装使用>介绍为微信支付的API封装及使用,其中介绍了如何配置好支付环境,并对扫码支付的两种方式如何在C#开发中使用进行 ...
- MySQL: Table 'mysql.plugin' doesn't exist的解决
安装解压版MySQL以后,不能启动,日志里面出现了这个错误: MySQL: Table 'mysql.plugin' doesn't exist 这是因为mysql服务启动时候找不到内置数据库&quo ...
- Scala 变长参数
如果Scala定义变长参数 def sum(i Int*), 那么调用sum时,可以直接输入sum(1,2,3,4,5) 但是不可以sum(1 to 5) 必须要将1 to 5 强制为seq sum( ...
- Android Weekly Notes Issue #226
Android Weekly Issue #226 October 9th, 2016 Android Weekly Issue #226 本期内容包括: 用Firebase做A/B Test; 用R ...
- Android 从零开始打造异步处理框架
转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/5995752.html 本文出自[赵彦军的博客] 概述 在Android中会使用异步任务来处理耗时操作,避免出 ...
- Linux系统安装MySql步骤及截屏
➠更多技术干货请戳:听云博客 如下是我工作中的记录,介绍的是linux系统下使用官方编译好的二进制文件进行安装MySql的安装过程和安装截屏,这种安装方式速度快,安装步骤简单! 需要的朋友可以按照如下 ...
- MP3文件ID3信息编辑器代码开源 - 开源研究系列文章
上次把磁性窗体的源码开源了,这次就开源另一个程序源码:MP3文件ID3信息编辑器.这个源码也比较简单,关键在于获取和写入MP3文件的这个ID3的信息即可. 这个操作信息编辑的就封装在MP3ID3.ba ...
- 前端开发---ppt展示页面评论区支持动态交互效果
1. 工程地址:https://github.com/digitalClass/web_page 网站发布地址: http://115.28.30.25:8029/ 2. 用到的技术主要还是jquer ...
- ab
ab is a tool for benchmarking your Apache Hypertext Transfer Protocol (HTTP) server. It is designed ...
- 表单中Readonly和Disabled的区别
1.readonly是要锁定这个控件,通过在界面上无法修改他(但是通过javascript可以修改他). 2.disabled和readonly有相同的地方也是可以锁定这个控件用户不能改变他的值,但是 ...