string字符串

通过对象来保存字符串
1: string字符串对象的赋值 
string str1,str2 = "name","zhou"
str1 = str2 string类重载了运算符operator=
c++:str1.assgin(str2,3,1)
2: string字符串对象的合并 str1 = str1 + str2
string字符串对象的部分合并
c语言:strncat(ch1,ch2,3)
c++:str1.append(str2,2,3)
3: string字符串的长度
c语言:strlen
c++:str1.size()计算的是可见字符的长度
4: string字符串对象的替换 
c语言:strncpy(ch1,ch2,4)用ch2的前四个字符替换掉ch1
c++:str1.replace(1,3,str2,2,3)
replace的重载形式支持char字符数组
replace的重载形式支持字符
5:string字符串对象的拷贝
c语言:memmove(ch1,ch2,3)第三个参数要拷贝的字符数目
str1.copy(ch1,10,0)从string对象的字符串中下标为0的位置拷贝10个元


素到字符串数组ch1中
6:string字符串插入
str1.insert(2,str2,0,4)从str1下标为2的位置开始插入
7:string字符串删除
str1.earse(2,4)
8:string字符串查找
c语言:strchr(ch1,'c')在ch1中查找字符'c'返回的是找到字符的地址
c++:str1.find('w',0)在str1中第0个位置开始查找字符'w'
输入查找的字符,固定查找的位置
9:string字符串比较
str1.compare该函数可以被重载
10:判断string字符串对象是否为空
str1.empty()
11:string字符串对象内容交换
c++:str1.swap(str2)
12:string字符串转换为char型字符串
str1.c_str()


13:结构体成员默认是公有的
14:重载ostream& operator<<(ostream &os, const A &a)
为什么要返回ostream& 和传递的参数是ostream&这时因为ostream没有复


制构造函数,不能按值返回,只能按引用返回
15:重载istream& operator<<(istream &os,A &a)
因为是输入所以第二个参数不能定义为const A &a
const对象只能能调用const函数,不能修改const对象的值
16:编写一个string类
当重载为类的成员函数,二元运算符只能带一个参数
class String
{
public:
String();
String(const char *const ch);
String(int  length);
int getLen()conat{return len;}
const char* getStr()const{return str;}
char& operator[](int length);
char& operator[](int length)const;
String& operator=(const String &s);
//复制构造函数中s只能调用const operator[]函数
String(const String &s);
~String(){delete []str; len = 0;}
//s用来输出,不会修改s加const
friend ostream& operator<<(ostream &o,const String &s)
{
s<<s.str<<endl;
return o;
}
//s用来保存用户的输入,所以要去掉const
friend istream& operator>>(istream &i,String &s)
{
i>>s.str;
return i;
}
friend bool operator<(const String &s1,const String &s2)
{
if (strcmp(s1.str,s2.str)<0)
{
return 1;
}
return 0;
}
friend bool operator>(const String &s1,const String &s2)
{
if (strcmp(s1.str,s2.str)>0)
{
return 1;
}
return 0;
}
friend bool operator==(const String &s1,const String &s2)
{
if (strcmp(s1.str,s2.str)==0)
{
return 1;
}
return 0;
}
String operator+(const String &s);
void operator+=(const String &s);
private:
char *str;
int len;
};
String::String()
{
cout<<"不带参数的构造函数"<<endl;
len = 0;
data = new char[1];
data[0] = '\0'
}
String::String(const char *const ch)
{
cout<<"带参数的构造函数"<<endl;
len = strlen(ch);
str = new char[len+1];
for (int i = 0; i < len; i++)
{
str[i] = ch[i]
}
str[len] = '\0';
}
char& String::operator[](int length)
{
if (length <0 || length>len)
return str[len-1]
else
return str[length]
}
char& String::operator[](int length)const
{
if (length <0 || length>len)
return str[len-1]
else
return str[length]
}
//String s1 = "name";String s2 = "mike"; 
s1 = s2会调用operator=运算符
String& String::operator=(const String &s)
{
if (*this == &s)
return *this;
delete []str;
len = s.getLen();
str = new char[len+1]
for (int i=0; i< len;i++)
{
str[i] = s[i];
}
str[len+1] = '\0';
return *this;
}
//复制构造函数中s只能调用const operator[]函数
String::String(const String &s)
{
len = s.getLen();
str = new char[len+1];
for (int i = 0; i < len; i++)
{
//s是一个对象s[i]重载了下标运算符函数
str[i] = s[i];
}
str[len] = '\0';
}
String::String(int  length)
{
len = length;
str = new char[len+1];
for (int i = 0 ; i <= len; i++)
{
str[i] = '\0';
}
}
String String::operator+(const String &s)
{
int total = len + s.getLen();
String temp(total);
int i = 0
for (i = 0; i < len; i++)
{
temp[i] = str[i];
}
for (int j = 0; j < s.getLen(); j++)
{
temp[i++] = s[j];
}
temp[i]='\0';
return temp;
}
void String::operator+=(const String &s)
{
int total = len + s.getLen();
String temp(total);
int i = 0
for (i = 0; i < len; i++)
{
temp[i] = str[i];
}
for (int j = 0; j < s.getLen(); j++)
{
temp[i++] = s[j];
}
temp[i]='\0';
*this = temp;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值