- #ifndef STRING
- #define STRING
- #include <iostream>
- using namespace std;
- class String
- {
- public:
- String();
- String(const char* s);
- String(const char* s, size_t n);
- String(size_t n, const char c);
- String(const String& str, size_t pos, size_t len);
- String(const String& str);
- String swap(String& str);
- char& at(size_t n);
- char const* c_str(void) const;
- String& operator=(const String& str);
- char& operator[](size_t i);
- char const& operator[](size_t i) const; // 按照this的const属性重载,常对象调用此版本
- friend String operator+(String& lhs, const String& rhs);
- friend String operator+ (String& lhs, const char* rhs);
- friend ostream& operator<<(ostream& os, String& str); // 流运算符只能用友元函数,friend写在声明中
- friend istream& operator>>(istream& is, String& str);
- friend bool operator==(const String& lhs, const String& rhs);
- friend bool operator!=(const String& lhs, const String& rhs);
- friend bool operator<(const String& lhs, const String& rhs);
- friend bool operator>(const String& lhs, const String& rhs);
- friend bool operator<=(const String& lhs, const String& rhs);
- friend bool operator>=(const String& lhs, const String& rhs);
- ~String();
- private:
- char *m;
- };
- String::String()
- {
- m = new char[1];
- m[0] = '\\0';
- }
- String::String(const char* s)
- {
- int n = strlen(s) + 1;
- m = new char[n];
- strcpy(m, s);
- }
- String::String(const char* s, size_t n)
- {
- m = new char[n + 1];
- strncpy(m, s, n);
- m[n] = '\\0';
- }
- String::String(size_t n, const char c)
- {
- m = new char[n + 1];
- size_t i;
- for (i = 0; i < n; i++)
- {
- m[i] = c;
- }
- m[i] = '\\0';
- }
- String::String(const String& str, size_t pos, size_t len)
- {
- m = new char[len + 1];
- strncpy(m, str.m + pos, len);
- m[len] = '\\0';
- }
- String::String(const String& str)
- {
- int n = strlen(str.m) + 1;
- m = new char[n];
- strcpy(m, str.m);
- }
- char& String::operator[](size_t i)
- {
- return m[i];
- }
- char const& String::operator[](size_t i) const
- {
- return const_cast<String&>(*this)[i];
- }
- String String::swap(String& str)
- {
- char* temp = m;
- m = str.m;
- str.m = temp;
- return *this;
- }
- ostream& operator<<(ostream &os, String &str)
- {
- os << str.m << endl;
- return os;
- }
- istream& operator>>(istream& is, String& str)
- {
- str.m = new char[100];
- is >> str.m;
- return is;
- }
- char& String::at(size_t n)
- {
- return m[n];
- }
- char const* String::c_str(void) const
- {
- return m;
- }
- String& String::operator=(const String& str)
- {
- if (*this == str)
- {
- return *this;
- }
- delete[] m;
- int n = strlen(str.m) + 1;
- m = new char[n];
- strcpy(m, str.m);
- }
- String operator+(String& lhs, const String& rhs)
- {
- size_t n = strlen(rhs.m) + 1 + strlen(lhs.m) + 1;
- lhs.m = (char *)realloc(lhs.m, n); // 注意分配足够多的内存!第一次bug在这,找了很久
- strcat(lhs.m, rhs.m);
- return lhs;
- }
- bool operator== (const String& lhs, const String& rhs)
- {
- return strcmp(lhs.m, rhs.m) == 0 ? true : false;
- }
- bool operator!= (const String& lhs, const String& rhs)
- {
- return !(strcmp(lhs.m, rhs.m) == 0 ? true : false);
- }
- bool operator<(const String& lhs, const String& rhs)
- {
- return strcmp(lhs.m, rhs.m) < 0 ? true : false;
- }
- bool operator>(const String& lhs, const String& rhs)
- {
- return strcmp(lhs.m, rhs.m) > 0 ? true : false;
- }
- bool operator<=(const String& lhs, const String& rhs)
- {
- return strcmp(lhs.m, rhs.m) <= 0 ? true : false;
- }
- bool operator>=(const String& lhs, const String& rhs)
- {
- return strcmp(lhs.m, rhs.m) >= 0 ? true : false;
- }
- String:: ~String()
- {
- delete[] m;
- m = NULL;
- }
- #endif // STRING
- //该片段来自于http://www.codesnippet.cn/detail/2811201514104.html
来源: http://www.codesnippet.cn/detail/2811201514104.html