string类主要方法
/*
构造函数
mystring() //生成一个空字符串s
mystring(char str[])
析构
s.~string() //销毁所有字符,释放内存
函数:
swap(string &s2) //交换两个字符串的内容
s1.insert(2,"u"); //第2个位置插入u
s1.assign("cao"); s2 = "aaaaa"; //赋以新值
s1.erase(0,2); //删除第0个位置开始到第2个位置之前 0,1 start=0,end=len
s1.clear(); //删除全部字符串
s1.append("sb"); //在末尾添加字符串
s1.replace(0,2,"hhhh"); //0到2之前 替换为hhhh
s1.length() //返回字符串长度
*/
因为有一个私有的指针成员函数,一个用new操作符创建,这个很重要,还要重载它的=运算符。
有些函数太难写了,如果运行不起就算了。
mystring.cpp
#include <iostream>
#include<cstring>
#include <string.h>
using namespace std;
class mystring{
private:
char *str;
int len;
public:
mystring(){
str = new char;
strcpy(str,"");
len = 0;
}
mystring(char const *s){
len = strlen(s);
str = new char[len];
strcpy(str,s);
}
char *data(){return str;}
~mystring(){delete [] str;}
mystring(const mystring &other)
{
str = new char[64];
memcpy(str, other.str, 64);
}
//友元 重载cout cin =
friend ostream &operator<<(ostream &output, const mystring &tmp){
output<<tmp.str;
return output;
}
friend istream &(operator>>)(istream &input,mystring &tmp){
input>>tmp.str;
tmp.len = strlen(tmp.str);
return input;
}
mystring &operator=(const mystring &s){
if(this->str != NULL){
delete [] str;
str = nullptr;
}
this->len = s.len;
str = new char[s.len];
for(int i=0; i<len; i++){
*(str+i) = *(s.str+i);
}
return *this;
}
friend mystring operator+(const mystring s1, const mystring s2){
char *p1 = s1.str;
char *p2 = s2.str;
char *tmp = p1;
while(*p1) p1++;
while(*p2){
*p1 = *p2++;
p1++;
}
*p1 = '\0';
p1 = tmp;
return mystring(p1);
}
mystring &operator=(char const *s){
if(this->str != NULL){
delete [] str;
str = nullptr;
}
this->len = strlen(s);
str = new char[len];
strcpy(str, s);
return *this;
}
//获取长度
int length(){return len;}
//清空字符串
void clear(){
strcpy(str, "");
len = 0;
}
//指定删除某一节 s1.erase(0,2);
int erase(int start, int end=0);
//swap(string &s2) //交换两个字符串的内容
void swap(mystring &s2);
//s1.append("sb"); //在末尾添加字符串
void append(const char *ptr);
//s1.replace(0,2,"hhhh"); //0到2之前 替换为hhhh
int replace(int start, int end, const char *ptr);
//s1.insert(2,"u"); //第2个位置插入u
int insert(int pos, const char *ptr);
};
int mystring::erase(int start, int end){
//判断位置是否有效
if(end==0)
end = len-1;
else
end -= 1;
if(start > end || start>len){
cout<<"参数异常,已停止操作"<<endl;
exit(-1);
}
char tmp[len]={0};
int j=0;
for(int i=0; i<len; i++){
if(i>=start && i<=end) continue;
tmp[j++] = *(str+i);
}
strcpy(str,tmp);
len = strlen(str);
return 0;
}
void mystring::swap(mystring &s2){
char *tmp = str;
int tmp_len = len;
str = s2.str;
len = s2.length();
s2.str = tmp;
s2.len = tmp_len;
}
void mystring::append(const char *ptr){
strcat(str, ptr);
len = strlen(str);
}
int mystring::replace(int start, int end, const char *ptr){
//判断位置是否正确 s3.replace(0,2,"ffffff");
if(start >= end || (end-start)>=len){
cout<<"位置不正确,已停止操作"<<endl;
exit(-1);
}
char *p = new char[64];
int i=0,j=0;
int ptr_len = strlen(ptr);
while(start--){
*(p+j) = *(str+i);
j++;
i++;
}
i = 0;
while(ptr_len--){
*(p+j) = *(ptr+i);
i++;
j++;
}
i = end;
while(*(str+i) != '\0'){
*(p+j) = *(str+i);
i++;
j++;
}
*(p+j) = '\0';
//cout<<"p = "<<p<<endl;
strcpy(str, p);
len = strlen(p);
delete [] p;
return 0;
}
int mystring::insert(int pos, const char *ptr){
if(pos<0 || pos>len){
cout<<"位置出错,已停止操作"<<endl;
exit(-1);
}
replace(pos+1,pos+2,ptr);
return 0;
}
测试
#include <iostream>
#include <string>
#include "mystring.cpp"
using namespace std;
int main()
{
mystring s1="abcd";
mystring s2="123";
mystring s3;
//重载 = +
s3 = s1 + s2;
cout<<"s3 = "<<s3<<endl; //输出流重载
//在末尾添加字符串
s3.append("world!");
//insert 插入
s3.insert(1,"???");
cout<<"s3 = "<<s3<<endl;
//replace 替换对应位置字符串
s3.replace(1,3,"###");
//erase 删除对应位置字符串 参数:开始位置 结束位置
s3.erase(0,1);
cout<<s3;
mystring s4;
//输入流重载
cin>>s4;
cout<<s4<<endl;
//交换s3 和 s4的内容
s3.swap(s4);
cout<<"s3="<<s3<<"\ts4="<<s4<<endl;
//获取长度
cout<<"s3 len="<<s3.length()<<endl;
cout<<"s4 len="<<s4.length()<<endl;
//清空
s4.clear();
cout<<"s4="<<s4<<endl;
return 0;
}
效果: