数据结构之串的处理(CString的实现)

我花一些时间写了一个CString类.单从功能上也没什么就是实现了我们写程序常用的一些功能.我认为关键还是在你写的时候,在你脑海中出现的一些思想!

这个CString类实现了大部分MFC里的CString功能,并且基本上是用纯粹的C++语言来实现的.如果你仔细看的话,你应该可以学到有用的算法,数据结构 ,以及编程思想的.

好了,我就无私的给他贴出来啊.如果有兴趣,你也可以继续扩充.并且,我也留了一点故意不实现的.好,看代码吧.

首先是头文件:


#include "stdarg.h"
#include "TCHAR.H"
#include "iostream"
using namespace std;

#ifndef _STRING_H_H_
#define _STRING_H_H_

#ifndef NULL
#define NULL 0
#endif//NULL

#ifndef DEF_SIZE
#define DEF_SIZE 256
#endif//DEF_SIZE
/*------------------------------------------------*/
声明:
 1、字符串默认空间大小256
 2、字符串是基于0的索引
/*------------------------------------------------*/
class CString
{
public:
 char * m_pStr;
 int  m_nLen;
 int  m_nMaxSize;

public:
 /*------------------------------------------------*/
       构造函数
 /*------------------------------------------------*/
 CString ( );
 CString ( const char ch );
 CString ( const char* pstr );
 CString ( const CString &str );

 /*------------------------------------------------*/
 析构函数
 /*------------------------------------------------*/
 virtual ~CString( );

 /*------------------------------------------------*/
 向串尾追加数据,返回长度,否则-1
 /*------------------------------------------------*/
 int Append ( const char ch );
 int Append ( const char *pstr );
 int Append ( const CString &str );

 /*------------------------------------------------*/
 插入数据到串的一个位置,返回长度,否则-1
 /*------------------------------------------------*/
 int Insert ( const char ch,int pos );
 int Insert ( const char *pstr,int pos );
 int Insert ( const CString &str,int pos );

 /*------------------------------------------------*/
 删除数据,返回长度,否则-1
 /*------------------------------------------------*/
 int  Remove ( int nStart, int nNum );

 /*------------------------------------------------*/
 截断数据,返回长度
 /*------------------------------------------------*/
 int Trim ( const char ch );
 int Trim ( const char *pstr );
 int TrimLeft ( const char ch );
 int TrimLeft ( const char *pstr );
 int TrimRight ( const char ch );
 int TrimRight ( const char *pstr );


 /*------------------------------------------------*/
 查找数据,返回长度,否则-1
 /*------------------------------------------------*/
 int Find ( const char ch, int nStart = 0 );
 int Find ( const char *pstr, int nStart = 0 );

 /*------------------------------------------------*/
 变换数据
 /*------------------------------------------------*/
 void MakeUpper ( );
 void MakeLower ( );
 void Reverse ( );

 /*------------------------------------------------*/
 反向查找数据,返回正向索引值,否则-1
 /*------------------------------------------------*/
 int ReverseFind ( const char ch, int nStart = 0 );
 int ReverseFind ( const char *pstr, int nStart = 0 );

 /*------------------------------------------------*/
 设置和得到数据
 /*------------------------------------------------*/
 char GetAt ( int pos );
 int  SetAt ( int pos ,const char ch );

 /*------------------------------------------------*/
 替换数据
 /*------------------------------------------------*/
 int  Replace ( const char ch1, const char ch2 );
/* 
 int  Replace ( const char ch, const char *pstr );
 int  Replace ( const char *pstr1, const char *pstr2 );
 int  Replace ( const char *pstr, const char ch );
 //呵呵,没有写了。谁有兴趣就写写哦!
*/
 /*------------------------------------------------*/
 其他
 /*------------------------------------------------*/
 int   GetLength ( );
 int   GetBufferSize ( );
 bool  IsEmpty ( );

 /*------------------------------------------------*/
 清空数据,返回BUFFER大小
 /*------------------------------------------------*/
 int   Clear();

 /*------------------------------------------------*/
 格式化数据
 /*------------------------------------------------*/
 void Format ( const char * pstrFormat, ... );

 /*------------------------------------------------*/
 操作符重载
 /*------------------------------------------------*/
 operator char * ();
 operator const char * ();


 void operator = ( const char ch );
 void operator = ( const char * pstr );
 void operator = ( const CString & str );

 friend CString operator + ( const char ch, const CString & str );
 friend CString operator + ( const char pstr[], const CString & str );
 friend CString operator + ( const CString & str1, const CString & str2 );

 void operator += ( const char ch );
 void operator += ( const char pstr[] );
 void operator += ( c

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值