C++面试题(二)——自己实现一个String类_普通网友的博客-CSDN博客
2.重载流函数及getline函数参考自:关于C++ 友元函数重载cin,cout,和+运算符的方法_jacka03的博客-CSDN博客
C++ 重载cout_快乐的提千万的博客-CSDN博客_重载cout
C++ getline函数用法_临渊慎行的博客-CSDN博客_c++ getline
c++中getline的用法_IT_xiaolaoshu的博客-CSDN博客_c++ getline
3.实现的功能包含:
拷贝构造函数、析构函数、拷贝赋值函数、
重载 +=运算符、数组索引运算符[]、输入输出运算符、
实现size()函数、
4.最终代码#pragma once //String.h #includeusing namespace std; class String { public: String(const String& str) :capacity(str.capacity), length(str.length) {//拷贝构造函数1 s = new char[capacity+4]; memcpy(s, str.s, length); } String(const char* ch) :capacity(defaultCapacity), length(strlen(ch)) {//拷贝构造函数2 //cout << length << endl; if (length > capacity) { capacity = length * 2; } s = new char[capacity + 4]; memcpy(s, ch, length); } String() :String(""){ } ~String() { try { delete[] s; } catch (exception e) { cerr << e.what() << endl; } } String& operator=(String& str) {//拷贝赋值函数1 length = str.length; if (length >= capacity) { capacity = length * 2; delete[] s; s = new char[capacity + 4]; } memcpy(s, str.s, length); return *this; } String& operator=(const char* ch) {//拷贝赋值函数2 length = strlen(ch); if (length >= capacity) { capacity = length * 2; delete[] s; s = new char[capacity+4]; } memcpy(s, ch, length); return *this; } String& operator+=(String& str) { length += str.length; if (length >= capacity) { capacity = length * 2; char* temp = s; s = new char[capacity + 4]; memcpy(s, temp, length); delete[] temp; } memcpy(s + length - str.length, str.s, length); return *this; } String& operator+=(const char* ch) { return *this += String(ch); } char operator[](int i) {//数组索引运算符 if (i >= length) { throw runtime_error("越界"); } return s[i]; } int size() {//字符串长度 return length; } private: int length; int capacity; char* s; int defaultCapacity = 2;//默认字符串容量 friend ostream& operator<<(ostream& out, String& str) {// ostream是系统自带cout的类型 for (int i = 0; i < str.length; ++i) { out << str[i]; } return out; } friend istream& operator>>(istream& in, String& str) {// istream是系统自带cin的类型 //char* ch = new char[1024]{}; char ch[1024]{}; in.getline(ch, 1024); int len = 0; for (int i = 0; i < 1024; ++i) { if (ch[i] == 0) {//读取输入长度 len = i+1; break; } } // char* temp = new char[len+1]; char temp[1024]; try { memcpy(temp, ch, len);//加这个可以使strlen读到正确的字符串长度 str = temp;//赋值给str } catch (exception e) { cout << e.what()< #include#include "String.h" using namespace std; int main() { String s= "123123";// = new char[6];//测试拷贝构造函数 cout << s.size() << endl;//测试容量 cout << sizeof("123") << endl; cout << s[2] << endl;//测试数组索引函数 cout << s << endl; s += "098";//测试+=函数 cout << s << endl; s = "333334";//测试拷贝赋值函数 cout << s << endl; cin >> s;//测试输入函数 cout << s << endl; s += "098";//测试+=函数 cout << s << endl; return 0; } 在实现的过程中还遇到了HEAP CORRUPTION DETECTED的错误的错误,参考了:
Heap Corruption Detected解决方法__北方的雪_的博客-CSDN博客
C++调试遇到HEAP CORRUPTION DETECTED的错误与解决方法_BlueBallonBird的博客-CSDN博客
然后在new的时候将capacity改成capacity+4便解决问题了,具体原因还未查出,应该是没考虑全字符串的尾巴还有给空格的问题。
改进后:
#pragma once //String.h #includeusing namespace std; class String { public: String(const String& str) :capacity(str.capacity), length(str.length) {//拷贝构造函数1 s = new char[capacity]; memcpy(s, str.s, length); } String(const char* ch) :capacity(defaultCapacity), length(strlen(ch)) {//拷贝构造函数2 //cout << length << endl; // cout << defaultCapacity << endl; if (length +1>= capacity) { capacity = (length + 1) * 2; } s = new char[capacity]; memcpy(s, ch, length); } String() :String(""){ } ~String() { try { delete[] s; } catch (exception e) { cerr << e.what() << endl; } } String& operator=(String& str) {//拷贝赋值函数1 length = str.length; if (length + 1 >= capacity) { capacity = (length+1) * 2; delete[] s; s = new char[capacity]; } memcpy(s, str.s, length); return *this; } String& operator=(const char* ch) {//拷贝赋值函数2 length = strlen(ch); if (length +1>= capacity) { capacity = (length + 1) * 2; delete[] s; cout << length <<"aaaa" < = capacity) { capacity = (length + 1) * 2; char* temp = s; s = new char[capacity];/ memcpy(s, temp, length); delete[] temp; } memcpy(s + length - str.length, str.s, length); return *this; } String& operator+=(const char* ch) { return *this += String(ch); } char operator[](int i) {//数组索引运算符 if (i >= length) { throw runtime_error("越界"); } return s[i]; } int size() {//字符串长度 return length; } private: int length; int capacity; char* s; int defaultCapacity = 3;//默认字符串容量 friend ostream& operator<<(ostream& out, String& str) {// ostream是系统自带cout的类型 for (int i = 0; i < str.length; ++i) { out << str[i]; } return out; } friend istream& operator>>(istream& in, String& str) {// istream是系统自带cin的类型 //char* ch = new char[1024]{}; char ch[1024]{}; in.getline(ch, 1024); int len = 0; for (int i = 0; i < 1023; ++i) {//最后一位为空格 if (ch[i] == 0) {//读取输入长度 len = i+1; break; } } // char* temp = new char[len+1]; char temp[1024]; try { memcpy(temp, ch, len);//加这个可以使strlen读到正确的字符串长度,注意-1 str = temp;//赋值给str } catch (exception e) { cout << e.what()<