C++ 基于类的String实现
2016-08-07 00:10
232 查看
STL中string字符串类有很多成员函数,在此文中,小编根据在学的数据结构写了一个简单的String类,基本函数功能与标准模板库的函数有所雷同。亮点在于将list单向链表转化成String对象进项String的操作。在写比较函数时可以充分利用C字符串转换函数将String对象的比较转换成C字符串的比较,简化了代码的书写。
代码如下://///// the achievement of class string/////////// #include <iostream> #include <list> #include <cstring> #include "List.h" using namespace std; class String { public: String(); ~String(); String(const String& copy); String( const char* copy); String(List<char>& copy); void operator=(const String& copy); const char* c_str() const; protected: char* entries; int length; }; bool operator == (const String &first, const String& second); bool operator > ( const String &first, const String& second); bool operator < ( const String &first, const String& second); bool operator >= (const String &first, const String& second); bool operator <= (const String &first, const String& second); bool operator != (const String &first, const String& second); String::String() { entries = NULL; length = 0; } String::String(const char* in_string) { length = strlen(in_string); entries = new char[length + 1]; strcpy(entries, in_string); } String::~String() { delete entries; } String::String(const String& copy) { length = copy.length; entries = new char[copy.length+1]; int i; for( i = 0; i < length; i++) entries[i] = copy.entries[i]; entries[i] = '\0'; } String::String(List<char>& in_list) { length = in_list.length(); entries = new char[length+1]; for( int i = 0; i < length; i++) entries[i] = in_list.getItem(i); entries[length] = '\0'; } void String::operator=(const String& copy) { int i; if(entries != NULL) delete entries; length = copy.length; entries = new char[copy.length+1]; for(i = 0; i < copy.length; i++) entries[i] = copy.entries[i]; entries[i] = '\0'; } const char* String::c_str() const { return (const char*) entries; } bool operator==(const String& first, const String &second) { return strcmp(first.c_str(), second.c_str() ) == 0; } bool operator > ( const String &first, const String& second) { return strcmp(first.c_str(), second.c_str()) > 0; } bool operator < ( const String &first, const String& second) { return strcmp(first.c_str(), second.c_str()) < 0; } bool operator >= ( const String &first, const String& second) { return strcmp(first.c_str(), second.c_str()) >= 0; } bool operator <= (const String& first, const String& second) { return strcmp(first.c_str(), second.c_str()) <= 0; } bool operator != (const String& first, const String& second) { return strcmp(first.c_str(), second.c_str()) != 0; } int main() { String str1 = "I'm a student studying in sun yatsen univesity"; String str2(str1); if( str1 == str2) cout<< "str1 == str2" <<endl; if( str1 > str2) cout << "str1 > str2" <<endl; }
写完之后,小编只测试了编译和个别函数。如果BUG发现,还望批评指出,不吝赐教。
相关文章推荐
- 基于ACE实现C++目录遍历
- c++中如何用string实现CString格式化的功能
- c++中如何用string实现CString格式化的功能
- 基于OPENGL使用C++实现相机类
- 基于特征分析谱估计算法(Capon, MUSIC, ESPRIT)的C++实现
- C/C++/C#是否支持基于string的switch-case??
- 用 C++ 实现基于 session 的权限管理系统
- 基于Visual C++6.0的DDL编程实现
- 连通区域标记:c++版的bwlabel实现(基于opencv)
- 基于C++有限状态机的实现技术(调查报告)
- 实现基于C++的动态事件机制
- VC++编译问题汇总1 单链表的表示和实现,基于c++
- 基于c++进程注入的实现
- C++基于链表泛型集合类的实现
- C++实现的huffman与canonical huffman的压缩解压缩系统,支持基于单词的压缩解压缩
- 基于C++有限状态机的实现技术(调查报告)
- C++实现CString和string的互相转换
- “基于关键字匹配的文本过滤系统”配置文件的设计和实现(C/C++源码)
- 基于Windows Socket的安全通信(C++实现,附源码)
- 基于数组实现双向栈 c++