代碼分析c++中string類-創(chuàng)新互聯(lián)

一:回顧

創(chuàng)新互聯(lián)公司從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元嵩明做網(wǎng)站,已為上家服務(wù),為嵩明各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108

(1)c++中的string類是在面試中和筆試中經(jīng)??嫉念}目; 工程代碼免費(fèi)下載 string類的自行實(shí)現(xiàn)

(2)c++中的string類和fstream類合起來(lái)是處理外部數(shù)據(jù)的利器;

(3)string類經(jīng)常用到find find_first_of find_first_not_of find_last_of find_last_not_of substr replace等,以及聯(lián)合使用來(lái)達(dá)到j(luò)ava中的split和trim

(4) 使用friend 僅僅是在類中進(jìn)行聲明的非內(nèi)部 卻可以訪問(wèn)內(nèi)部成員的外部函數(shù),而且在外部不再需要friend關(guān)鍵字;它與成員函數(shù)的區(qū)別是,friend和外部函數(shù)不含有this對(duì)象指針;本文用到了const 定義的全局大值最小值變量(代替#define)

(5) 有些函數(shù)返回的是MyString& 、Char& 等(引用),MyString、Char 等(傳值)這得看你返回的對(duì)象是函數(shù)的局部變量還是全局變量(或者類當(dāng)前對(duì)象成員變量);前者只能返回一個(gè)MyString、Char 等;后者強(qiáng)烈建議返回MyString& 、Char& 等(引用);

(6)有些函數(shù)的參數(shù)是const MyString& ,有些是MyString& (引用);這是為什么?前者是把外部值傳提到子函數(shù)內(nèi)部,且不允許改變;后者是作為函數(shù)的返回值傳遞進(jìn)去的,返回的結(jié)果為函數(shù)的處理結(jié)果(而不用函數(shù)自身返回值了)。

二:下面是簡(jiǎn)單的實(shí)現(xiàn)了一下string類,參照的是STL源碼,但是自己理解的還是不夠深,難免有一些錯(cuò)誤,請(qǐng)各位指教

(1)MyString.h文件

#ifndef MYSTRING_H
#define MYSTRING_H
#include "MyExcept.h"
#include <cstring>
#include <iostream>
const int INI_MAX = 0x7fffffff;//2^32npos
const int INI_MIN = 0x80000000;// -2^32
const int npos = 0xffffffff;// npos
using namespace std;

class MyString
{
  public:
  // constructor
  MyString();//
  MyString(const MyString &);//
  MyString(const char *);
  MyString(const size_t,const char);
  // destructor
  ~MyString();
  // attributes

  size_t length();// 字符串長(zhǎng)度
  bool isEmpty();// 返回字符串是否為空
  const char* c_str();// 返回c風(fēng)格的trr的指針
  // friend funs
  // read writer operations
  friend ostream& operator<< (ostream&, const MyString&);
  friend istream& operator>> (istream&, MyString&);
  //add operation
  friend MyString operator+(const MyString&,const MyString&);
  // compare operations
  friend bool operator==(const MyString&,const MyString&);
  friend bool operator!=(const MyString&,const MyString&);
  friend bool operator<(const MyString&,const MyString&);
  friend bool operator<=(const MyString&,const MyString&);
  friend bool operator>(const MyString&,const MyString&);
  friend bool operator>=(const MyString&,const MyString&);
  // 成員函數(shù)實(shí)現(xiàn)運(yùn)算符重載,其實(shí)一般需要返回自身對(duì)象的,成員函數(shù)運(yùn)算符重載會(huì)好一些
  // index operation
  char& operator[](const size_t);
  const char& operator[](const size_t)const;
  // =
  MyString& operator=(const MyString&);
  // +=
  MyString& operator+=(const MyString&);
  // +=
  //MyString operator+=(const MyString&); cannot be overloaded
  // 成員操作函數(shù)
  // substr
  MyString substr(size_t pos,const size_t n);
  // append
  MyString& append(const MyString&);
  //insert
  MyString& insert(size_t,const MyString&);
  //assign 替換
  MyString& assign(MyString&,size_t,size_t);
  // erase 刪除
  MyString& erase(size_t,size_t);
  //find_first_of 查找某一個(gè)字符 size_t 是非符號(hào)數(shù)的,重載
  // 查找在字符串中第一個(gè)與str中的某個(gè)字符匹配的字符,返回它的位置。
  //搜索從index開始,如果沒(méi)找到就返回string::npos
  int find_first_of(const char* str,size_t index=0);
  int find_first_of(const char ch,size_t index=0);
  int find_first_of(const MyString &,size_t index=0);
  // 在字符串中查找第一個(gè)與str中的字符都不匹配的字符,返回它的位置。搜索從index開始。如果沒(méi)找到就返回string::nops
  int find_first_not_of(const char* str,size_t index=0);
  int find_first_not_of(const char ch,size_t index=0);
  int find_first_not_of(const MyString&,size_t index=0);
  // swap
  void swap(MyString& lhs,MyString& rhs);
  // replace_all
  MyString& replace_all(const char oldc,const char newc=NULL);
  MyString& replace(size_t index,size_t num1,size_t num2,const char ch);
  //find
  int find(const char* str,size_t index=0);
  int find(const MyString& str,size_t index=0);
  int find(const char ch,size_t index=0);


  //private
  private:
  char *p_str;
  size_t strLength;
};
#endif // MYSTRING_H

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站bm7419.com,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

分享名稱:代碼分析c++中string類-創(chuàng)新互聯(lián)
當(dāng)前地址:http://bm7419.com/article26/hcscg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、虛擬主機(jī)、網(wǎng)站排名、手機(jī)網(wǎng)站建設(shè)企業(yè)網(wǎng)站制作、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)