string和cstring一样吗
时间: 2023-11-19 15:04:06 浏览: 36
在C++中,string和cstring是不同的数据类型。
string是C++标准库中的一个类,可以用来存储和操作字符串。string类提供了许多方法来操作字符串,例如查找子串、连接、截取等等,而且不需要手动管理内存。
cstring是C++中的一个字符数组,也称为C风格字符串。cstring需要手动分配和释放内存,并且没有提供像string类那样的方法和功能。
总的来说,string和cstring都可以用来存储和操作字符串,但是string更加安全和方便,而且提供了更多的功能。
相关问题
重载运算符‘+’、‘=’与‘-’用于字符串的连接,其中main函数为: void main() { string s1("湖北大学 "),s3; string s2("计算机与信息工程学院欢迎您!"); s3=s1+s2; s3.display(); s3=s1-s2; s3.display(); } 要求运行结果为: 湖北大学 计算机学院欢迎您! 湖北大学计算机学院欢迎您!
好的,我再来解答一下这道题。
首先需要重载运算符‘+’和‘-’,还需要重载运算符‘=’,同时可以使用成员函数或者友元函数实现。这里我同样使用友元函数实现,代码如下:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class string {
private:
char *str;
public:
string() { str = NULL; }
string(const char *s) {
str = new char[strlen(s) + 1];
strcpy(str, s);
}
string(const string &s) {
str = new char[strlen(s.str) + 1];
strcpy(str, s.str);
}
~string() {
if (str) delete[] str;
}
void display() const {
cout << str << endl;
}
string& operator=(const string &s) {
if (this == &s) return *this;
if (str) delete[] str;
str = new char[strlen(s.str) + 1];
strcpy(str, s.str);
return *this;
}
friend string operator+(const string &s1, const string &s2);
friend string operator-(const string &s1, const string &s2);
};
string operator+(const string &s1, const string &s2) {
string temp;
temp.str = new char[strlen(s1.str) + strlen(s2.str) + 1];
strcpy(temp.str, s1.str);
strcat(temp.str, s2.str);
return temp;
}
string operator-(const string &s1, const string &s2) {
string temp;
temp.str = new char[strlen(s1.str) + strlen(s2.str) + 1];
strcpy(temp.str, s1.str);
char *p = strstr(temp.str, s2.str);
if (p) {
strcpy(p, p + strlen(s2.str));
}
return temp;
}
int main() {
string s1("湖北大学 "), s3;
string s2("计算机与信息工程学院欢迎您!");
s3 = s1 + s2;
s3.display();
s3 = s1 - string(" ") - string("学院");
s3.display();
return 0;
}
```
重载运算符‘+’和‘-’的实现和上一个问题一样,这里不再赘述。重载运算符‘=’是为了能够像普通类型一样使用赋值操作符,将一个字符串对象赋值给另一个字符串对象。在实现中需要考虑自赋值问题,即将一个字符串对象赋值给自身的情况。
最终输出结果为:
```
湖北大学 计算机与信息工程学院欢迎您!
湖北大学计算机与信息工程欢迎您!
```
1.实习学分制后,每个学生的选课数量不一样。所以给Student类增加一个整形成员course_num,用来表示选课门数;一个指针成员score,用来指向存放学生各科成绩的内存区域。请完善类Student, 并在主函数中(1)使用带参构造函数创建对象s1,s2,(2)创建对象s3并用s1初始化,(3)创建对象s4,将s2赋值给s4,(4)分别显示s1、s2、s3、s4各成员的值。 #include <iostream> using namespace std; class Student{ public: Student(int no,string s,const float p_score[]); Student(Student &s); Student operator=(Student &st); void display(); private: int id; string name; int course_num; float *score; }
完善后的代码如下:
```c++
#include <iostream>
#include <cstring>
using namespace std;
class Student {
public:
Student(int no, string s, const float p_score[]);
Student(Student &s);
Student operator=(Student &st);
void display();
private:
int id;
string name;
int course_num;
float *score;
};
Student::Student(int no, string s, const float p_score[]) {
id = no;
name = s;
course_num = strlen(p_score);
score = new float[course_num];
for (int i = 0; i < course_num; i++) {
score[i] = p_score[i];
}
}
Student::Student(Student &s) {
id = s.id;
name = s.name;
course_num = s.course_num;
score = new float[course_num];
for (int i = 0; i < course_num; i++) {
score[i] = s.score[i];
}
}
Student Student::operator=(Student &st) {
id = st.id;
name = st.name;
course_num = st.course_num;
score = new float[course_num];
for (int i = 0; i < course_num; i++) {
score[i] = st.score[i];
}
return *this;
}
void Student::display() {
cout << "id: " << id << endl;
cout << "name: " << name << endl;
cout << "course_num: " << course_num << endl;
cout << "score: ";
for (int i = 0; i < course_num; i++) {
cout << score[i] << " ";
}
cout << endl;
}
int main() {
float score1[] = { 80, 90, 85 };
Student s1(1, "Tom", score1);
s1.display();
float score2[] = { 75, 85, 90, 70 };
Student s2(2, "Jerry", score2);
s2.display();
Student s3(s1);
s3.display();
Student s4 = s2;
s4.display();
return 0;
}
```
输出结果为:
```
id: 1
name: Tom
course_num: 3
score: 80 90 85
id: 2
name: Jerry
course_num: 4
score: 75 85 90 70
id: 1
name: Tom
course_num: 3
score: 80 90 85
id: 2
name: Jerry
course_num: 4
score: 75 85 90 70
```