String类学习

String类简单实现

String类负责对字符串的存储和处理,可以用char*、字符数组等来表示,但最好还是用STL中的string类型。在C++中,string类型的头文件为#include<string>,其中,'string.h'和'cstring'都不是string类的头文件。这两个头文件主要定义C风格的字符串操作方法,比如说,strlen(),strcpy()等。'string.h'是C风格的头文件格式,’cstring‘是C++风格头文件,作用和'string.h'一样,目的是为了和C兼容。

String类的实现如下:

class String {
public:
	String(const char* str = NULL);//构造函数
	String(const String& other);//拷贝构造函数
	~String();//析构函数
	String& operator =(const String& other);//赋值函数
	String& operator +(const String& other);//字符串连接
	bool operator ==(const String& other);//判断字符串是否相等
	int getLength();
private:
	char* data;
};

其中,各成员函数实现如下:

String::String(const char* str) {
	if (str == NULL) {
		data = new char[1];
		*data = '\0';
	}
	else {
		int length = strlen(str);
		data = new char[length + 1];
		strcpy(data, str);
	}
}
String::String(const String& other) {
	if (!other.data) {
		data = 0;
	}
	data = new char[strlen(other.data) + 1];
	strcpy(data, other.data);
}
String::~String() {
	if (data) {
		delete[] data;
		data = 0;
	}
}
String& String::operator =(const String& other) {
	if (this == &other) {
		return *this;
	}
	else {
		delete[] data;
		if (!other.data) {
			data = 0;
		}
		else {
			data = new char[strlen(other.data) + 1];
			strcpy(data, other.data);
		}
		return *this;
	}
}
String& String::operator+(const String& other) {
	String newString;
	if (!other.data) {
		newString = *this;
	}
	else if (!data) {
		newString = other;
	}
	else {
		newString.data = new char[strlen(data) + strlen(other.data) + 1];
		strcpy(newString.data, data);
		strcat(newString.data, other.data);
	}
	return newString;
}
bool String::operator==(const String& other) {
	if (strlen(data) != strlen(other.data)) return false;
	else {
		return strcmp(data, other.data) ? false : true;
	}
}
int String::getLength() {
	return strlen(data);
}

c++字符串和c字符串的转换

c++提供的转换为c字符串的函数有:data()、c_str()和copy(),其中,data()以字符数组形式返回字符串内容,末尾没有'\0',c_str()同样返回字符数组,但是以'\0'结尾。copy()是把字符串的内容写入既有的c_string或者字符数组中。
对于c_str(),调用它时会产生一个const char*指针,它的使用例子如下:
int main(){
    string str = "piaopiao9393"
    const char* cstr = str.c_str();
    cout<<cstr<<endl;
    str = "9393piaopiao"
    cout<<cstr<<endl;
    return 0;
}
这两次cstr的输出分别为'piaopiao9393'和'9393piaopiao',就是说,使用c_str()方法,当我改变str的值时,cstr的值也会跟着改变。为了防止这种情况发生,可以考虑先把数据复制出来,如下:
int main(){
	string str = "piaopiao9393";
	char* cstr = new char[81];
	strncpy(cstr,str.c_str(),str.size());
	cout<<cstr<<endl;
	str = "9393piaopiao";
	cout<<cstr<<endl;
}
上面这段代码,cstr的输出分别为'piaopiao9393'和'piaopiao9393'。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++的string类是C++标准库中提供的一个用于处理字符串的类。它提供了一系列成员函数和操作符重载,使得字符串的操作更加方便和高效。 string类的特点包括: 1. 动态内存管理:string类会自动管理字符串的内存,无需手动分配和释放内存。 2. 可变性:string对象可以随时修改其内容,包括插入、删除、替换等操作。 3. 字符串操作:string类提供了丰富的字符串操作函数,如查找、比较、连接、截取等。 以下是一些常用的string类成员函数和操作符重载: 1. 构造函数:可以使用不同的方式创建string对象,如默认构造函数、拷贝构造函数、从C风格字符串构造等。 2. 赋值操作:可以使用赋值运算符=将一个string对象赋值给另一个对象。 3. 连接操作:可以使用+运算符将两个string对象连接起来。 4. 访问字符:可以使用下标运算符[]或at()函数来访问字符串中的单个字符。 5. 获取长度:可以使用length()或size()函数获取字符串的长度。 6. 查找子串:可以使用find()函数在字符串中查找指定的子串。 7. 插入和删除:可以使用insert()函数在指定位置插入字符或子串,使用erase()函数删除指定位置的字符或子串。 8. 截取子串:可以使用substr()函数截取指定位置和长度的子串。 9. 比较字符串:可以使用比较运算符==、!=、<、>等来比较两个字符串的大小。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值