自己模擬寫C++中的String類型實(shí)例講解
下面是模擬實(shí)現(xiàn)字符串的相關(guān)功能,它包括一下功能:
String(const char * s);//利用字符串來初始化對(duì)象
String(); //默認(rèn)構(gòu)造函數(shù)
String(const String & s);//復(fù)制構(gòu)造函數(shù),利用String類型來初始化對(duì)象
~String(); //析構(gòu)函數(shù)
int length(); //返回String類型中字符串的長(zhǎng)度
String & operator=(const String & s);//重載=運(yùn)算符。
String & operator=(const char *);
char & operator[](int i); //重載【】運(yùn)算符
const char & operator[](int i) const;
friend bool operator<(const String & st,const String & st2);//重載<運(yùn)算符,用來比較String類型中字符串的大小。
friend bool operator>(const String & st,const String & st2);
friend bool operator==(const String & st,const String & st2);//重載==運(yùn)算符,判斷兩個(gè)String對(duì)象是否相等
friend ostream & operator<<(ostream & os,const String & st2);//重載輸出函數(shù)
friend istream & operator>>(istream & is,String & st2);//重載輸入函數(shù)
static int HowMang()//返回總共生成的String類對(duì)象的數(shù)目。
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會(huì)為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類型實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持我們。
欄 目:C語言
下一篇:數(shù)據(jù)結(jié)構(gòu)與算法 排序(冒泡,選擇,插入)
本文標(biāo)題:自己模擬寫C++中的String類型實(shí)例講解
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1352.html
您可能感興趣的文章
- 01-10大數(shù)(高精度數(shù))模板(分享)
- 01-10C++大數(shù)模板(推薦)
- 01-10快速模式匹配算法(KMP)的深入理解
- 01-10深入串的模式匹配算法(普通算法和KMP算法)的詳解
- 01-10C++ 模版雙向鏈表的實(shí)現(xiàn)詳解
- 01-10ACE反應(yīng)器(Reactor)模式的深入分析
- 01-10用代碼和UML圖化解設(shè)計(jì)模式之橋接模式的深入分析
- 01-10linux c模擬ls命令詳解
- 01-10下標(biāo)操作符重載模擬多維數(shù)組詳解
- 01-10淺析C++中模板的那點(diǎn)事


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法