最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年07月20日 08:39:46   投稿:jingxian  
下面小編就為大家?guī)硪黄约耗M寫C++中的String類型實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

下面是模擬實(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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • c++下使用windows api遍歷指定文件夾及其子文件夾中的文件

    c++下使用windows api遍歷指定文件夾及其子文件夾中的文件

    這篇文章主要介紹了c++下使用windows api遍歷指定文件夾及其子文件夾中的文件實(shí)現(xiàn)代碼,一般都是通過c++自帶的函數(shù)實(shí)現(xiàn)
    2021-07-07
  • c++ lambda捕獲this 導(dǎo)致多線程下類釋放后還在使用的錯(cuò)誤問題

    c++ lambda捕獲this 導(dǎo)致多線程下類釋放后還在使用的錯(cuò)誤問題

    Lambda表達(dá)式是現(xiàn)代C++的一個(gè)語(yǔ)法糖,挺好用的。但是如果使用不當(dāng),會(huì)導(dǎo)致內(nèi)存泄露或潛在的崩潰問題,這里總結(jié)下c++ lambda捕獲this 導(dǎo)致多線程下類釋放后還在使用的錯(cuò)誤問題,感興趣的朋友一起看看吧
    2023-02-02
  • C++?折疊參數(shù)包詳解(悄然增強(qiáng)編程效率)

    C++?折疊參數(shù)包詳解(悄然增強(qiáng)編程效率)

    折疊參數(shù)就是一個(gè)參數(shù)包, 代表是多個(gè)未知,tuple元組就是一個(gè)折疊參數(shù)的使用,這篇文章主要介紹了C++?折疊參數(shù)包悄然增強(qiáng)編程效率,需要的朋友可以參考下
    2023-05-05
  • C++中std::distance使用方法示例

    C++中std::distance使用方法示例

    std::distance?是 C++ 標(biāo)準(zhǔn)庫(kù)中的一個(gè)函數(shù),用于計(jì)算兩個(gè)迭代器之間的距離,本文主要介紹了C++中std::distance使用方法示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-04-04
  • C語(yǔ)言繪制簡(jiǎn)單時(shí)鐘小程序

    C語(yǔ)言繪制簡(jiǎn)單時(shí)鐘小程序

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言繪制簡(jiǎn)單時(shí)鐘小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C語(yǔ)言如何實(shí)現(xiàn)可變參數(shù)詳解

    C語(yǔ)言如何實(shí)現(xiàn)可變參數(shù)詳解

    這種可變參數(shù)可以說是C語(yǔ)言一個(gè)比較難理解的部分,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言如何實(shí)現(xiàn)可變參數(shù)的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • C++ Boost Container庫(kù)示例詳細(xì)講解

    C++ Boost Container庫(kù)示例詳細(xì)講解

    Boost是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱。Boost庫(kù)是一個(gè)可移植、提供源代碼的C++庫(kù),作為標(biāo)準(zhǔn)庫(kù)的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱
    2022-11-11
  • Qt讀寫XML文件的方法詳解(含源碼+注釋)

    Qt讀寫XML文件的方法詳解(含源碼+注釋)

    XML文件可以用來存儲(chǔ)項(xiàng)目中的數(shù)據(jù),它相當(dāng)于一個(gè)簡(jiǎn)單的數(shù)據(jù)庫(kù),下面這篇文章主要給大家介紹了關(guān)于Qt讀寫XML文件(含源碼+注釋)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-10-10
  • C++采用openfilename打開文件對(duì)話框用法實(shí)例

    C++采用openfilename打開文件對(duì)話框用法實(shí)例

    這篇文章主要介紹了C++采用openfilename打開文件對(duì)話框用法實(shí)例,是C++文件操作中非常實(shí)用的技巧,需要的朋友可以參考下
    2014-10-10
  • C語(yǔ)言獲取Shell返回結(jié)果的實(shí)現(xiàn)方法

    C語(yǔ)言獲取Shell返回結(jié)果的實(shí)現(xiàn)方法

    下面小編就為大家?guī)硪黄狢語(yǔ)言獲取Shell返回結(jié)果的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-07-07

最新評(píng)論

西乌| 蕉岭县| 拉孜县| 河南省| 平潭县| 永靖县| 沾化县| 隆子县| 永靖县| 镇雄县| 子长县| 灵石县| 开原市| 兴和县| 尉犁县| 台北县| 龙陵县| 杨浦区| 丹东市| 安化县| 静海县| 阜宁县| 措美县| 庄河市| 鲁山县| 十堰市| 大余县| 霍山县| 栾川县| 蓬莱市| 广昌县| 札达县| 宁明县| 德阳市| 新蔡县| 石台县| 偏关县| 绥江县| 南木林县| 牟定县| 呼玛县|