C++ - Implement a string class with basic functionality
2012-07-07 22:04
751 查看
// ------String.h------ #ifndef STRING_H #define STRING_H #include <iostream> using namespace std; class String { public: String(); String(int n, char c); String(const char* source); String(const String& s); // String& operator = (char* s); String& operator = (const String& s); ~String(); char& operator [] (int i){return a[i];} const char& operator [] (int i) const {return a[i];} // Indexer to constant. String& operator += (const String& s); int length(); friend istream& operator >> (istream& is, String& s); // Try to figure out why set ">>" friend function member. // friend bool operator < (const String& left,const String& right); friend bool operator > (const String& left, const String& right); // These three operators is not necessary to set them friend. Here is just for simplification. friend bool operator == (const String& left, const String& right); friend bool operator != (const String& left, const String& right); private: char* a; int size; }; #endif
// ------String.cpp------ #include "String.h" #include <cstring> #include <cstdlib> String::String() { a = new char[1]; a[0] = '\0'; size = 0; } String::String(int n, char c) { a = new char[n + 1]; memset(a, c, n); a = '\0'; size = n; } String::String(const char* source) { if(source == NULL) { a = new char[1]; a[0] = '\0'; size = 0; } else { size = strlen(source); a = new char[size + 1]; strcpy(a, source); } } String::String(const String& s) { size = strlen(s.a); a = new char[size + 1]; strcpy(a,s.a); } String& String::operator = (const String& s) { if(this == &s) return *this; else { delete[] a; size = strlen(s.a); a = new char[size + 1]; strcpy(a, s.a); return *this; } } String::~String() { delete[] a; } String& String::operator += (const String& s) { int j = strlen(a); int size = j + strlen(s.a); char* tmp = new char[size + 1]; strcpy(tmp, a); strcpy(tmp + j, s.a); delete[] a; a = tmp; return *this; } int String::length() { return strlen(a); }
// ------Main.cpp------ #include <iostream> #include "String.h" using namespace std; bool operator == (const String& left, const String& right) { int a = strcmp(left.a, right.a); if(a == 0) { return true; } else { return false; } } bool operator != (const String& left, const String& right) { return !(left == right); } ostream& operator << (ostream& os, String& s) { int length = s.length(); for(int i = 0; i < length; i++) { os << s[i]; } return os; } String operator + (const String& a, const String& b) { String temp; temp = a; temp += b; return temp; } bool operator < (const String& left, const String& right) { int j = 0; while((left[j] != '\0') && (right[j] != '\0')) { if(left[j] < right[j]) { return true; } else { if(left[j] == right[j]) { j++; continue; } else { return false; } } } if((left[j] == '\0') && (right[j] != '\0')) { return true; } else { return false; } } bool operator > (const String& left, const String& right) { int a = strcmp(left.a, right.a); if(a > 0) { return true; } else { return false; } } istream& operator >> (istream& is, String& s) { delete[] s.a; s.a = new char[20]; int m = 20; char c; int i = 0; while(is.get(c) && isspace(c)); if(is) { do { s.a[i] = c; i++; if(i == m - 1) { s.a[i] = '\0'; char* b = new char[m]; strcpy(b, s.a); m = m * 2; s.a = new char[m]; strcpy(s.a, b); delete[] b; } } while (is.get(c) && !isspace(c)); if(is) { is.unget(); } } s.size = i; s.a[i] = '\0'; return is; } int main() { String a = "abcd"; String b = "www"; String c(6, 'l'); String d; String e = a; String f; cin >> f; String g; g = a + b; if(a < b) { cout << "a < b" << endl; } else { cout << "a >= b" << endl; } if(e == a) { cout << "e == a" << endl; } else { cout << "e != a" << endl; } b += a; cout << a << endl; cout << b << endl; cout << c << endl; cout << d << endl; cout << e << endl; cout << f << endl; cout << g << endl; cout << g[0] << endl; return 0; } // Output: /* t a < b e == a abcd wwwabcd llllll abcd t abcdwww a */
相关文章推荐
- C++ 不能通过‘...’传递有不能平凡复制的类型‘const string {aka const class std::basic_string<char>}’
- class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<ch
- String class by C++
- C++ String class Examples and Tutorial
- ClassNotFoundException: org.apache.commons.lang.xwork.StringUtils with Struts2 - JSON - JQuery
- Send string in PUT request with libcurl in C++
- LINK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_string的解决方案
- [LinkedIn]Implement Find and replace (find given pattern and replace it with a given string)
- [C++] * Basic and Class
- There is no getter for property named 'column' in 'class java.lang.String'] with root cause
- ALT 工程中,当导出类中包含string类型的私有成员时,会产生告警 warning C4251:class“std::basic_string”需要有 dll 接口由 class“xxx”的客户端使用
- How to convert int to string with C++ - forest的日志 - 网易博客
- 使用log4cplus时遇到的链接错误:无法解析的外部符号 "public: static class log4cplus::Logger __cdecl log4cplus::Logger::getInstance(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,
- class std::basic_string 找到一个或多个多重定义的符号
- error C2079: 'lstr' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>'
- 【C++】实现一个简洁版的class String
- [C++] Hex string convert to integer with stringstream
- Trim a string with C++
- terminating with uncaught exception of type std::logic_error: basic_string::_S_construct NULL not va
- Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined