c++ 构造string 类
来源:互联网 发布:肖申克的救赎知乎 编辑:程序博客网 时间:2024/06/06 15:41
// 头文件
#pragma once#include <iostream>using namespace std;class CSimString{friend ostream &operator<<( ostream &, const CSimString & );private:char* pBuf;int len;public:CSimString(void);CSimString(const char* );CSimString(CSimString &);~CSimString(void);const CSimString& operator =(const CSimString &);const CSimString& operator +=(const CSimString &);bool operator ==(const CSimString& ) const;bool operator !=(const CSimString& ) const;bool operator <(const CSimString& ) const;char operator [](int index) const;CSimString operator()(int index,int subLen) const;const char* c_str() const;int getLen(void) const;};
//cpp文件
#include "stdafx.h"#include "SimString.h"#include <string.h>CSimString::CSimString(void):pBuf(NULL),len(0){pBuf= new char[1];pBuf[0] = '\0';}CSimString::CSimString(const char* str):pBuf(NULL),len(0){if (str){len = strlen(str);pBuf = new char[len+1];strcpy(pBuf,str);}else{pBuf = new char[1];pBuf[0] = '\0';}}CSimString::CSimString(CSimString& strObj):pBuf(NULL),len(0){if (strObj.pBuf){len=strlen(strObj.pBuf);pBuf = new char[len+1];strcpy(pBuf,strObj.pBuf);}else{pBuf = new char[1];pBuf[0] = '\0';}}CSimString::~CSimString(void){if (pBuf){delete pBuf;pBuf = NULL;}}const CSimString& CSimString::operator =(const CSimString& strObj){if (this != &strObj){delete pBuf;pBuf = NULL;if (strObj.pBuf){len = strlen(strObj.pBuf);pBuf = new char[len+1];strcpy(pBuf,strObj.pBuf);}else{pBuf = new char[1];pBuf[0]='\0';}}elsereturn *this;}const CSimString& CSimString::operator +=(const CSimString& strObj){int newLen = len + strObj.len;char *tmpPtr = new char[newLen +1];strcpy(tmpPtr,pBuf);strcpy(tmpPtr+len,strObj.pBuf);delete pBuf;pBuf = tmpPtr;len = newLen;return *this;}bool CSimString::operator ==(const CSimString& strObj) const{return strcmp(pBuf,strObj.pBuf) ==0;}bool CSimString::operator != (const CSimString& strObj) const{return !(*this == strObj);}bool CSimString::operator <(const CSimString& strObj) const{return strcmp(this->pBuf,strObj.pBuf)<0;}char CSimString::operator [](int index) const{if (index <0 || index>len){return '\0';}return pBuf[index];}CSimString CSimString::operator()(int index,int subLen) const{if (index <0 || index >len || subLen >len){return "";}int tmpLen;if (subLen ==0 ||index+subLen >len){tmpLen = len-index;}elsetmpLen = subLen;char *tmpPtr = new char[tmpLen +1];strncpy(tmpPtr,&pBuf[index],tmpLen);tmpPtr[tmpLen]='\0';CSimString newStr(tmpPtr);delete tmpPtr;return newStr;}const char* CSimString::c_str() const{return this->pBuf;}ostream& operator <<(ostream& output, const CSimString &ss){return output<<ss.pBuf;}int CSimString::getLen(void) const{return len;}
测试:
#include "stdafx.h"#include "SimString.h"int _tmain(int argc, _TCHAR* argv[]){CSimString tmp(NULL);CSimString tmp2("fsfdsfds");CSimString tmp3(tmp2);CSimString tmp4(tmp3);CSimString str1("123456789");CSimString str2("34");CSimString str3("12");bool m = (str1 == str3);CSimString str4 = str1(3,9);std::cout<<str4<<endl;return 0;}
0 0
- C++String构造函数
- String 类构造函数
- c++ 构造string 类
- String类的构造
- String类的构造
- String类的构造
- String 类的构造
- string 类的构造
- String类构造器
- c/c++中string类型的构造
- c++/string的构造析构函数
- C++String类型、内存分区、构造函数
- 【C#】自己写构造string.Join()方法
- String类:构造函数,拷贝构造函数
- 【C/C++】实现一个string类的构造函数、析构函数和赋值函数
- string类的简单构造。
- string类的构造函数
- 类String的构造函数
- raid介绍
- Hadoop作业的生命周期
- 108km创始人梁伟国:Unity3D游戏UI开发经验谈
- asp + html 留言例子
- more命令
- c++ 构造string 类
- 安卓 方向感应器加震动
- vision 引擎 游戏对象 实体
- iOS-调用系统的短信和发送邮件功能,实现短信分享邮件分享
- 现在公开一个DHT网络爬虫网络爬虫供大家一起交流
- IOS 应用中 打开 电话, 邮箱, 网站等URL
- 些代码准则:应高做到那些标准?
- java MinaServer
- 嵌入式 hi3518a裸板在nandflash上烧写uboot以及kernel以及文件系统