国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

自己模擬寫C++中的String類型實例講解

2020-01-26 14:00:40
字體:
來源:轉載
供稿:網友

下面是模擬實現字符串的相關功能,它包括一下功能:

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類型實例講解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石门县| 弥勒县| 盐津县| 诏安县| 繁峙县| 淮滨县| 无为县| 安宁市| 张北县| 黄山市| 安远县| 郯城县| 京山县| 年辖:市辖区| 苏州市| 株洲县| 泰和县| 松桃| 平塘县| 柳河县| 土默特左旗| 怀远县| 布拖县| 泽州县| 苍山县| 将乐县| 威远县| 图木舒克市| 仁寿县| 长葛市| 刚察县| 抚宁县| 清原| 华坪县| 嘉定区| 沅陵县| 龙井市| 潼南县| 四会市| 盐亭县| 元朗区|