自己模拟写C++中的String类型实例讲解
下面是模拟实现字符串的相关功能,它包括一下功能:
String(const char * s);//利用字符串来初始化对象
String(); //默认构造函数
String(const String & s);//复制构造函数,利用String类型来初始化对象
~String(); //析构函数
int length(); //返回String类型中字符串的长度
String & operator=(const String & s);//重载=运算符。
String & operator=(const char *);
char & operator[](int i); //重载【】运算符
const char & operator[](int i) const;
friend bool operator<(const String & st,const String & st2);//重载<运算符,用来比较String类型中字符串的大小。
friend bool operator>(const String & st,const String & st2);
friend bool operator==(const String & st,const String & st2);//重载==运算符,判断两个String对象是否相等
friend ostream & operator<<(ostream & os,const String & st2);//重载输出函数
friend istream & operator>>(istream & is,String & st2);//重载输入函数
static int HowMang()//返回总共生成的String类对象的数目。
String.h:
#ifndef STRING_H_INCLUDED #define STRING_H_INCLUDED #include"iostream" #include<string.h> using std::ostream; using std::istream; class String{ private: char * str; int len; public: static int num_strings; static const int CINLM=80; String(const char * s); String(); String(const String & s); ~String(); int length(); String & operator=(const String & s); String & operator=(const char *); char & operator[](int i); const char & operator[](int i) const; friend bool operator<(const String & st,const String & st2); friend bool operator>(const String & st,const String & st2); friend bool operator==(const String & st,const String & st2); friend ostream & operator<<(ostream & os,const String & st2); friend istream & operator>>(istream & is,String & st2); static int HowMang() { return num_strings; } }; #endif // STRING_H_INCLUDED
String.cpp:
#include<iostream> #include"String.h" #include"string.h" using namespace std; int String::num_strings=0; int String::length() { return this->len; } String::String(const char * s) { len=strlen(s); str=new char[len+1]; num_strings++; } String::String() { str=0; len=0; num_strings++; } String::String(const String & s) { num_strings++; len=strlen(s.str); str=new char[len+1]; strcpy(str,s.str); } String::~String() { --num_strings; delete [] str; len=0; } String & String::operator=(const String & s) { if(this==&s) return *this; delete [] str; len=strlen(s.str); str=new char[len+1]; strcpy(str,s.str); // num_strings++; } String & String::operator=(const char * s) { len=strlen(s); str=new char[len+1]; strcpy(str,s); // num_strings++; } char & String::operator[](int i) { return str[i]; } const char & String::operator[](int i) const { return str[i]; } bool operator<(const String & st,const String & st2) { if(strcmp(st.str,st2.str)<0) return true; else return false; } bool operator>(const String & st,const String & st2) { return (st<st2)==false; } bool operator==(const String & st,const String & st2) { if(strcmp(st.str,st2.str)>0) return true; else return false; } ostream & operator<<(ostream & os,const String & st2) { os<<st2.str; return os; } istream & operator>>(istream & is,String & st2) { char temp[String::CINLM]; is.get(temp,String::CINLM); if(is) st2=temp; while(is&&is.get()!='\n') continue; return is; }
main.cpp:
#include <iostream> #include"String.h" using namespace std; int main() { String name[5]; char temp[10]; int i; for(i=0;i<5;i++) { cin.get(temp,10); while(cin&&cin.get()!='\n') continue; if(!cin&&temp[0]=='\0')//如果是空串的话,cin会为false break; else name[i]=temp; } int total=i; int firs=0,shor=0; if(total<0) { cout<<"没有输入"<<endl; }else{ for(i=0;i<total;i++) { cout<<name[i][0]<<":"<<name[i]<<endl; } for(i=0;i<total;i++) { if(name[i]<name[firs]) firs=i; if(name[i].length()<name[shor].length()) shor=name[i].length(); } } cout<<"最短的字符串为:"<<name[shor]<<endl; cout<<"最前面的字符串为:"<<name[firs]<<endl; return 0; }
以上这篇自己模拟写C++中的String类型实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
您可能感兴趣的文章
- 01-10大数(高精度数)模板(分享)
- 01-10C++大数模板(推荐)
- 01-10快速模式匹配算法(KMP)的深入理解
- 01-10深入串的模式匹配算法(普通算法和KMP算法)的详解
- 01-10C++ 模版双向链表的实现详解
- 01-10ACE反应器(Reactor)模式的深入分析
- 01-10用代码和UML图化解设计模式之桥接模式的深入分析
- 01-10linux c模拟ls命令详解
- 01-10下标操作符重载模拟多维数组详解
- 01-10浅析C++中模板的那点事
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-10C#中split用法实例总结
- 01-10SublimeText编译C开发环境设置
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-11ajax实现页面的局部加载
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 04-02jquery与jsp,用jquery
- 01-10delphi制作wav文件的方法