c++ 构造string 类

// 头文件

#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';
		}
	}
	else
		return *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;
	}
	else
		tmpLen = 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值