一:回想
(1)c++中的string类是在面试中和笔试中常常考的题目; project代码免费下载 string类的自行实现
(2)c++中的string类和fstream类合起来是处理外部数据的利器;
(3)string类经经常使用到find find_first_of find_first_not_of find_last_of find_last_not_of substr replace等,以及联合使用来达到java中的split和trim
(4) 使用friend 不过在类中进行声明的非内部 却能够訪问内部成员的外部函数,并且在外部不再须要friendkeyword;它与成员函数的差别是,friend和外部函数不含有this对象指针;本文用到了const 定义的全局最大值最小值变量(取代#define)
(5) 有些函数返回的是MyString& 、Char& 等(引用),MyString、Char 等(传值)这得看你返回的对象是函数的局部变量还是全局变量(或者类当前对象成员变量);前者仅仅能返回一个MyString、Char 等;后者强烈建议返回MyString& 、Char& 等(引用);
(6)有些函数的參数是const MyString& ,有些是MyString& (引用);这是为什么?前者是把外部值传提到子函数内部,且不同意改变;后者是作为函数的返回值传递进去的,返回的结果为函数的处理结果(而不用函数自身返回值了)。
二:以下是简单的实现了一下string类,參照的是STL源代码,可是自己理解的还是不够深,难免有一些错误,请各位不吝赐教
(1)MyString.h文件
#ifndef MYSTRING_H
#define MYSTRING_H
#include "MyExcept.h"
#include <cstring>
#include <iostream>
const int INI_MAX = 0x7fffffff;//2^32npos
const int INI_MIN = 0x80000000;// -2^32
const int npos = 0xffffffff;// npos
using namespace std;
class MyString
{
public:
// constructor
MyString();//
MyString(const MyString &);//
MyString(const char *);
MyString(const size_t,const char);
// destructor
~MyString();
// attributes
size_t length();// 字符串长度
bool isEmpty();// 返回字符串是否为空
const char* c_str();// 返回c风格的trr的指针
// friend funs
// read writer operations
friend ostream& operator<< (ostream&, const MyString&);
friend istream& operator>> (istream&, MyString&);
//add operation
friend MyString operator+(const MyString&,const MyString&);
// compare operations
friend bool operator==(const MyString&,const MyString&);
friend bool operator!=(const MyString&,const MyString&);
friend bool operator<(const MyString&,const MyString&);
friend bool operator<=(const MyString&,const MyString&);
friend bool operator>(const MyString&,const MyString&);
friend bool operator>=(const MyString&,const MyString&);
// 成员函数实现运算符重载,事实上一般须要返回自身对象的,成员函数运算符重载会好一些
// index operation
char& operator[](const size_t);
const char& operator[](const size_t)const;
// =
MyString& operator=(const MyString&);
// +=
MyString& operator+=(const MyString&);
// +=
//MyString operator+=(const MyString&); cannot be overloaded
// 成员操作函数
// substr
MyString substr(size_t pos,const size_t n);
// append
MyString& append(const MyString&);
//insert
MyString& insert(size_t,const MyString&);
//assign 替换
MyString& assign(MyString&,size_t,size_t);
// erase 删除
MyString& erase(size_t,size_t);
//find_first_of 查找某一个字符 size_t 是非符号数的,重载
// 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。
//搜索从index開始,假设没找到就返回string::npos
int find_first_of(const char* str,size_t index=0);
int find_first_of(const char ch,size_t index=0);
int find_first_of(const MyString &,size_t index=0);
// 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index開始。假设没找到就返回string::nops
int find_first_not_of(const char* str,size_t index=0);
int find_first_not_of(const char ch,size_t index=0);
int find_first_not_of(const MyString&,size_t index=0);
// swap
void swap(MyString& lhs,MyString& rhs);
// replace_all
MyString& replace_all(const char oldc,const char newc=NULL);
MyString& replace(size_t index,size_t num1,size_t num2,const char ch);
//find
int find(const char* str,size_t index=0);
int find(const MyString& str,size_t index=0);
int find(const char ch,size_t index=0);
//private
private:
char *p_str;
size_t strLength;
};
#endif // MYSTRING_H(2)MyString.cpp文件
#include "MyString.h"
#include <cassert>
// constructor
MyString::MyString():p_str(NULL),strLength(0){}
MyString::MyString(const MyString &str)//
{
if(NULL == str.p_str)
{
return;
}
strLength = str.strLength;
p_str = new char[strLength+1];
strcpy(p_str,str.p_str);
}
MyString::MyString(const char *str)
{
if(NULL == str)
{
return;
}
strLength = strlen(str);
p_str = new char[strLength+1];
strcpy(p_str,str);
}
MyString::MyString(const size_t len,const char ch)
{
if(NULL == ch)
{
return;
}
strLength = len;
p_str = new char[strLength+1];
for(size_t i=0;i<strLength;i++)
{
p_str[i] = ch;
}
p_str[strLength] = '