自己模擬寫C++中的String類型實(shí)例講解
下面是模擬實(shí)現(xiàn)字符串的相關(guān)功能,它包括一下功能:
String(const char * s);//利用字符串來(lái)初始化對(duì)象
String(); //默認(rèn)構(gòu)造函數(shù)
String(const String & s);//復(fù)制構(gòu)造函數(shù),利用String類型來(lái)初始化對(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)算符,用來(lái)比較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<<"沒(méi)有輸入"<<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++中string轉(zhuǎn)換為char*類型返回后亂碼問(wèn)題解決
- C++中將string類型轉(zhuǎn)化為int類型
- 關(guān)于C++ string和c類型字符數(shù)組的對(duì)比
- C++利用stringstream進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換實(shí)例
- 利用C++實(shí)現(xiàn)從std::string類型到bool型的轉(zhuǎn)換
- C++如何通過(guò)ostringstream實(shí)現(xiàn)任意類型轉(zhuǎn)string
- 用標(biāo)準(zhǔn)c++實(shí)現(xiàn)string與各種類型之間的轉(zhuǎn)換
- 淺談C++中的string 類型占幾個(gè)字節(jié)
- C++中的string類型
相關(guān)文章
C++實(shí)現(xiàn)分水嶺算法(Watershed Algorithm)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)分水嶺算法Watershed Algorithm,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一 下2018-01-01VS2019中CMake項(xiàng)目的簡(jiǎn)單使用方法
這篇文章主要介紹了VS2019中CMake項(xiàng)目的簡(jiǎn)單使用方法,需要的朋友可以參考下2020-02-02Java?C++?算法題解leetcode669修剪二叉搜索樹(shù)示例
這篇文章主要為大家介紹了Java?C++?算法題解leetcode669修剪二叉搜索樹(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09C語(yǔ)言中的強(qiáng)符號(hào)和弱符號(hào)介紹
這篇文章主要介紹了C語(yǔ)言中的強(qiáng)符號(hào)和弱符號(hào)介紹,本文用多個(gè)實(shí)例來(lái)講解強(qiáng)符號(hào)和弱符號(hào),需要的朋友可以參考下2015-03-03