C++ string类的实现
string类的简单实现
/* * String类的简单实现 * Build 20160701 * Author: LonelyEnvoy * Copyright (c) 2015-2016 * All Rights Reserved. */ #define MAX_LENGTH 50 #define NULL 0 #include <iostream> using std::ostream; using std::istream; using std::cout; using std::endl; class String { public: String(const char* s = NULL); String(const String &obj); ~String(); void set(char* s); char* get() const; void print() const; int size() const; static int strlen(const char* s); static char* strcat(char* s1, const char* s2); static char* strcpy(char* s1, const char* s2); String operator+(String& obj2); String& operator=(String& obj2); String& operator=(const char* s2); String& operator+=(String& obj2); char& operator[](unsigned n); friend ostream & operator << (ostream & os, const String & obj); friend istream & operator >> (istream & is, String & obj); private: char* str; };
String::String(const char * s) { if (s == NULL) str = NULL; else { str = new char[strlen(s) + 1]; strcpy(str, s); } } String::String(const String & obj) { if (obj.str == NULL) str = NULL; else { str = new char[strlen(obj.str) + 1]; strcpy(str, obj.str); } } String::~String() { if (str == NULL) return; delete[]str; } void String::set(char * s) { if (s == NULL) return; if (str != NULL) delete[]str; str = new char[strlen(s) + 1]; int i; for (i = 0; i < strlen(s); i++) str[i] = s[i]; s[i] = '\0'; } char * String::get() const { return str; } void String::print() const { for (int i = 0; i < strlen(str); i++) cout << str[i]; cout << endl; } int String::size() const { return strlen(str); } int String::strlen(const char * s) { if (s == NULL) return 0; int i = 0; while (s[++i] != '\0'); return i; } char * String::strcat(char * s1, const char * s2) { // s1 must have enough space for storage if (s1 == NULL || s2 == NULL) return NULL; int i, j; for (i = strlen(s1), j = 0; j < strlen(s2); i++, j++) s1[i] = s2[j]; s1[i] = '\0'; return s1; } char * String::strcpy(char * s1, const char * s2) { // s1 must have enough space for storage if (s1 == NULL || s2 == NULL) return NULL; int i; for (i = 0; i < strlen(s2); i++) s1[i] = s2[i]; s1[i] = '\0'; return s1; } String String::operator+(String & obj2) { String s; if (str == NULL || obj2.str == NULL) return String(); else if (str == NULL) s = obj2; else if (obj2.str == NULL) s = *this; else { s.str = new char[strlen(str) + strlen(obj2.str) + 1]; strcpy(s.str, str); strcat(s.str, obj2.str); } return s; } String & String::operator=(String & obj2) { if (this != &obj2) { delete[]str; if (obj2.str == NULL) str = NULL; else { str = new char[strlen(obj2.str) + 1]; strcpy(str, obj2.str); } } return obj2; } String & String::operator=(const char * s2) { String temp(s2); operator=(temp); return temp; } String & String::operator+=(String & obj2) { operator=(operator+(obj2)); return obj2; } char & String::operator[](unsigned n) { if (n >= 0 && n < (unsigned)size()) return str[n]; throw 1; // 越界抛出异常 } ostream & operator<<(ostream & os, const String & obj) { os << obj.str; return os; } istream & operator>>(istream & is, String & obj) { char temp[MAX_LENGTH]; is >> temp; obj = temp; return is; }