c++模擬實(shí)現(xiàn)string類(lèi)詳情
一、string類(lèi)簡(jiǎn)介
標(biāo)準(zhǔn)庫(kù)類(lèi)型string
表示可變長(zhǎng)的字符序列,使用string類(lèi)型必須首先包含string頭文件。作為標(biāo)準(zhǔn)庫(kù)的一部分,string定義在命名空間std
中。
二、模擬實(shí)現(xiàn)
成員變量
?? ?char* _str; ?? ?size_t _size; ?? ?size_t _capacity;?? ?//不包含最后做標(biāo)識(shí)的'\0' ?? ?static const size_t npos;
_str
用來(lái)存儲(chǔ)字符串,_size表示字符串有效數(shù)據(jù)個(gè)數(shù),_capacity表示容量大小,其中不包含最后做標(biāo)識(shí)的‘\0’
。
例如這樣一段代碼:string str(“hello”);
npos
是一個(gè)靜態(tài)成員常量,值為-1,因?yàn)閟ize_t是無(wú)符號(hào)整數(shù)類(lèi)型,所以它表示size_t類(lèi)型的最大值。
當(dāng)npos用在做成員函數(shù)中l(wèi)en的參數(shù)時(shí),表示“直到字符串的結(jié)尾”,
例如用來(lái)刪除字符串中某一部分的函數(shù)erase(size_t pos = 0, size_t len = npos)
,如果沒(méi)傳參數(shù)len,那么就會(huì)從pos位置直接刪到最后。
當(dāng)作為返回值時(shí),npos通常表示不匹配,例如find函數(shù)返回npos,就意味著沒(méi)找到。
初始化string對(duì)象常用的幾種方式:
?? ?string s1("hello");?? ??? ? //默認(rèn)初始化,s1是一個(gè)空字符串 ?? ?string s2 = s1; ? ? ? ? ?//s2是s1的副本 ?? ?string s3 = "hello"; ?? ?string s4(5, 'c'); ? ? ? //s4的內(nèi)容是ccccc
成員函數(shù)
構(gòu)造函數(shù):
?string(const char* str = "") ?? ??? ?:_size(strlen(str)) ?? ??? ?,_capacity(_size) ?? ?{ ?? ??? ?_str = new char[_capacity + 1]; ?? ??? ?strcpy(_str, str); ?? ?}
這里需要注意的是參數(shù)列表的初始化順序與初始化列表列出的順序無(wú)關(guān),只與它在類(lèi)中聲明順序有關(guān),由于我們聲明成員變量順序_size
在_capacity
前面,所以這里_size也要在_capacity前面。
容量_capacity
中不包含’\0’,所以申請(qǐng)空間時(shí)多申請(qǐng)一位。
重載一個(gè)用來(lái)初始化s4的構(gòu)造函數(shù)
?string(const size_t n, const char ch) ?? ??? ?:_size(n) ?? ??? ?, _capacity(_size) ?? ?{ ?? ??? ?_str = new char[_capacity + 1]; ?? ??? ?for (size_t i = 0; i < n; ++i) ?? ??? ?{ ?? ??? ??? ?_str[i] = ch; ?? ??? ?} ?? ??? ?_str[_size] = '\0'; ?? ?}
拷貝構(gòu)造:
?? ?string(const string& s) ?? ??? ?:_str(nullptr) ?? ?{ ?? ??? ?string tmp(s._str); ?? ??? ?swap(tmp); ?? ?}
用s._str
去構(gòu)造臨時(shí)對(duì)象tmp,這里引用傳參s是s1的別名,tmp調(diào)用構(gòu)造函數(shù)開(kāi)空間拷貝數(shù)據(jù),所以最后tmp和s1是一樣的數(shù)據(jù)一樣的大小,而tmp的空間是s2想要的,所以把他們交換。這樣s2就達(dá)到深拷貝的效果了。
tmp是局部對(duì)象,出函數(shù)作用域會(huì)調(diào)用析構(gòu)函數(shù),而s2的_str指向的位置是隨機(jī)值,把tmp和s2交換后tmp的_str就變成了隨機(jī)值,不能對(duì)一個(gè)隨機(jī)的位置進(jìn)行釋放,所以先在參數(shù)列表中把s2的_str指向nullptr
。
另外還需要提供一個(gè)swap函數(shù)交換兩個(gè)對(duì)象:
?? ?void swap(string& s) ?? ?{ ?? ??? ?::swap(_str, s._str); ?? ??? ?::swap(_size, s._size); ?? ??? ?::swap(_capacity, s._capacity); ?? ?}
賦值重載:
?? ?string& operator=(string s) ?? ?{ ?? ??? ?swap(s); ?? ??? ?return *this; ?? ?} string s1(“hello”); string s2(“world”); s2 = s1;
s1傳值傳參給s,調(diào)用拷貝構(gòu)造深拷貝,s和s1是一樣的,把s和s2交換,出函數(shù)作用域后形參s調(diào)用析構(gòu)函數(shù)釋放資源。
析構(gòu)函數(shù):
?? ?~string() ?? ?{ ?? ??? ?delete[] _str; ?? ??? ?_str = nullptr; ?? ??? ?_size = _capacity = 0; ?? ?}
迭代器
迭代器是一個(gè)像指針一樣的東西,有可能是指針,也有可能不是指針。
begin()
返回第一個(gè)有效數(shù)據(jù)位置的迭代器end()
返回最后一個(gè)有效數(shù)據(jù)的下一個(gè)位置的迭代器vector/string
這種底層用連續(xù)一段空間存儲(chǔ)數(shù)據(jù),支持[ ] + 下標(biāo)訪(fǎng)問(wèn),迭代器用原生指針即可。
普通迭代器:
?? ?typedef char* iterator; ?? ? ?? ?iterator begin() ?? ?{ ?? ??? ?return _str; ?? ?} ?? ? ?? ?iterator end() ?? ?{ ?? ??? ?return _str + _size; ?? ?}
const迭代器:
?typedef const char* const_iterator; ?? ? ?? ?const_iterator begin() const ?? ?{ ?? ??? ?return _str; ?? ?} ?? ? ?? ?const_iterator end() const ?? ?{ ?? ??? ?return _str + _size; ?? ?}
重載運(yùn)算符[ ]
也一樣提供非const
版本和const
版本
?char& operator[](size_t pos) ?? ?{ ?? ??? ?assert(pos < _size); ?? ??? ?return _str[pos]; ?? ?} ?? ? ?? ?const char& operator[](size_t pos) const ?? ?{ ?? ??? ?assert(pos < _size); ?? ??? ?return _str[pos]; ?? ?}
現(xiàn)在可以創(chuàng)建一個(gè)string
對(duì)象并且遍歷了。
三、幾種常見(jiàn)函數(shù)
reserve()
reserve()
函數(shù)用來(lái)修改字符串容量的大小。如果申請(qǐng)空間的newcapacity
大于當(dāng)前的capacity,則分配新的存儲(chǔ)空間,并使capacity
等于或大于 newcapacity。如果newcapacity小于當(dāng)前容量,則是一個(gè)非綁定收縮請(qǐng)求。
從C++20起如果newcapacity
小于或等于當(dāng)前容量,則沒(méi)有效果。
? ?void reserve(size_t n) ?? ?{ ?? ??? ?if (n > _capacity) ?? ??? ?{ ?? ??? ??? ?char* tmp = new char[n + 1]; ?? ??? ??? ?strcpy(tmp, _str); ?? ??? ??? ?delete[] _str; ?? ??? ??? ?_str = tmp; ?? ??? ??? ?_capacity = n; ?? ??? ?} ?? ?}
resize()
resize()
用來(lái)將字符串大小調(diào)整為n個(gè)字符的長(zhǎng)度。
如果 n 小于當(dāng)前字符串長(zhǎng)度,則將當(dāng)前size縮短為n。
如果 n 大于當(dāng)前字符串長(zhǎng)度,則通過(guò)在末尾插入所需數(shù)量的字符來(lái)擴(kuò)展當(dāng)前內(nèi)容,以達(dá)到 n 的大小。 如果指定了字符,則將新元素初始化為該字符,否則初始化為空字符。
?void resize(size_t n, char ch = '\0') ?? ?{ ?? ??? ?if (n < _size) ?? ??? ?{ ?? ??? ??? ?_str[n] = '\0'; ?? ??? ??? ?_size = n; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?if (n > _capacity) ?? ??? ??? ?{ ?? ??? ??? ??? ?reserve(n); ?? ??? ??? ?} ?? ??? ??? ?for (size_t i = _size; i < n; ++i) ?? ??? ??? ?{ ?? ??? ??? ??? ?_str[i] = ch; ?? ??? ??? ?} ?? ??? ??? ?_size = n; ?? ??? ??? ?_str[_size] = '\0'; ?? ??? ?} ?? ?}
push_back()
將給定字符加到字符串的末尾。
?void push_back(char ch) ?? ?{ ?? ??? ?if (_size >= _capacity) ?? ??? ?{ ?? ??? ??? ?size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity; ?? ??? ??? ?reserve(newcapacity); ?? ??? ?} ?? ??? ?_str[_size] = ch; ?? ??? ?++_size; ?? ??? ?_str[_size] = '\0';?? ? ?? ?}
append()
在字符串結(jié)尾添加字符串
?void append(const char* str) ?? ?{ ?? ??? ?size_t len = strlen(str); ?? ??? ?if (_size + len > _capacity) ?? ??? ?{ ?? ??? ??? ?reserve(_size + len); ?? ??? ?} ?? ??? ?strcpy(_str + _size, str); ?? ??? ?_size += len; ?? ?}
有了push_back()
和append()
就很方便重載+=
重載+=
右操作數(shù)為字符:
? ?string& operator+=(char ch) ?? ?{ ?? ??? ?push_back(ch); ?? ??? ?return *this; ?? ?}
右操作數(shù)為字符串:
?? ?string& operator+=(const char* str) ?? ?{ ?? ??? ?append(str); ?? ??? ?return *this; ?? ?}
右操作數(shù)為對(duì)象:
?? ?string& operator+=(const string& s) ?? ?{ ?? ??? ?*this += s._str; ?? ??? ?return *this; ?? ?}
insert()
在字符串任意位置插入一個(gè)字符或字符串。push_back()
和append()
都可以復(fù)用insert()
。
插入字符:
?? ?string& insert(size_t pos, char ch) ?? ?{ ?? ??? ?assert(pos <= _size); ?? ??? ?if (_size == _capacity) ?? ??? ?{ ?? ??? ??? ?size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity; ?? ??? ??? ?reserve(newcapacity); ?? ??? ?} ?? ??? ?for (size_t i = _size + 1; i > pos; --i) ?? ??? ?{ ?? ??? ??? ?_str[i] = _str[i - 1]; ?? ??? ?} ?? ??? ?_str[pos] = ch; ?? ??? ?++_size; ?? ??? ?return *this; ?? ?}
插入字符串:
?string& insert(size_t pos, const char* str) ?? ?{ ?? ??? ?assert(pos <= _size); ?? ??? ?size_t len = strlen(str); ?? ??? ?if (len + _size > _capacity) ?? ??? ?{ ?? ??? ??? ?reserve(len + _size); ?? ??? ?} ?? ??? ?for (size_t i = _size + len; i >= (pos + len); --i) ?? ??? ?{ ?? ??? ??? ?_str[i] = _str[i - len]; ?? ??? ?} ?? ??? ?for (size_t i = 0; i < len; ++i) ?? ??? ?{ ?? ??? ??? ?_str[pos + i] = str[i]; ?? ??? ?} ?? ??? ?_size += len; ?? ??? ?return *this; ?? ?}
erase()
刪除字符串的一部分,減少它的長(zhǎng)度,如果沒(méi)給參數(shù)len就會(huì)從pos位置直接刪到最后。
?string& erase(size_t pos = 0, size_t len = npos) ?? ?{ ?? ??? ?assert(pos < _size); ?? ??? ?if (len >= (_size - pos)) ?? ??? ?{ ?? ??? ??? ?_str[pos] = '\0'; ?? ??? ??? ?_size = pos; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?for (size_t i = pos + len; i <= _size; ++i) ?? ??? ??? ?{ ?? ??? ??? ??? ?_str[i - len] = _str[i]; ?? ??? ??? ?} ?? ??? ??? ?_size -= len; ?? ??? ?} ?? ??? ?return *this; ?? ?}
find()
查找從pos
位置開(kāi)始第一個(gè)給定字符或字符串。找到了返回對(duì)應(yīng)字符或子串第一個(gè)字符第一次出現(xiàn)的位置,沒(méi)找到返回npos。
查找字符:
?size_t find(char ch, size_t pos = 0) ?? ?{ ?? ??? ?for (size_t i = pos; i < _size; ++i) ?? ??? ?{ ?? ??? ??? ?if (_str[i] == ch) ?? ??? ??? ??? ?return i; ?? ??? ?} ?? ??? ?return npos; ?? ?}
查找字符串:
? ?size_t find(const char* sub, size_t pos = 0) ?? ?{ ?? ??? ?const char* p = strstr(_str + pos, sub); ?? ??? ?if (p) ?? ??? ??? ?return p - _str; ?? ??? ?return npos; ?? ?}
四、操作符重載
流插入<<
?? ?ostream& operator<<(ostream& out, const string& s) ?? ?{ ?? ??? ?for (size_t i = 0; i < s.size(); ++i) ?? ??? ?{ ?? ??? ??? ?out << s[i]; ?? ??? ?} ?? ??? ?return out; ?? ?}
流提取>>
?? ?istream& operator>>(istream& in, string& s) ?? ?{ ?? ??? ?s.clear(); ?? ??? ?char ch = in.get(); ?? ??? ?while (ch != ' ' && ch != '\n') ?? ??? ?{ ?? ??? ??? ?s += ch; ?? ??? ??? ?ch = in.get(); ?? ??? ?} ?? ??? ?return in; ?? ?}
其他運(yùn)算符重載和一些簡(jiǎn)單的函數(shù)在完整代碼給出。
到此這篇關(guān)于c++模擬實(shí)現(xiàn)string類(lèi)詳情的文章就介紹到這了,更多相關(guān)c++模擬實(shí)現(xiàn)string類(lèi) 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言拓展實(shí)現(xiàn)Lua sleep函數(shù)
這篇文章主要介紹了C語(yǔ)言拓展實(shí)現(xiàn)Lua sleep函數(shù),本文使用C語(yǔ)言寫(xiě)出sleep函數(shù),編譯后在Lua中調(diào)用,需要的朋友可以參考下2015-04-04C++常用的11種設(shè)計(jì)模式解釋及示例代碼詳解
c++常用的設(shè)計(jì)模式包括單例模式、工廠模式、抽象工廠模式、適配器模式、裝飾者模式、代理模式、外觀模式、橋接模式、組合模式、享元模式、觀察者模式和命令模式等,這篇文章主要介紹了C++常用的11種設(shè)計(jì)模式解釋及示例,需要的朋友可以參考下2023-02-02C語(yǔ)言關(guān)鍵字const和指針的結(jié)合使用
這篇文章主要介紹了C語(yǔ)言關(guān)鍵字const和指針的結(jié)合,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02深入分析Visual C++進(jìn)行串口通信編程的詳解
本篇文章是對(duì)Visual C++進(jìn)行串口通信編程進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05從匯編看c++中函數(shù)里面的static關(guān)鍵字的使用說(shuō)明
c++中的static關(guān)鍵字使得函數(shù)里面的局部變量的存活期不在局限于函數(shù)里面,而是變?yōu)樵谡麄€(gè)程序生命期里面都有效2013-05-05OpenCV識(shí)別提取圖像中的水平線(xiàn)與垂直線(xiàn)
這篇文章主要為大家詳細(xì)介紹了OpenCV識(shí)別提取圖像中的水平線(xiàn)與垂直線(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07在Visual Studio Code中配置C++編譯環(huán)境的問(wèn)題
關(guān)于Visual Studio Code對(duì)C++環(huán)境的配置方法應(yīng)該有好多種,我這里用到了其中的兩種,具體內(nèi)容詳情文中給大家詳細(xì)介紹,對(duì)Visual Studio Code配置C++編譯環(huán)境相關(guān)知識(shí)感興趣的朋友一起看看吧2021-07-07C語(yǔ)言每日練習(xí)之求兩個(gè)矩陣的乘積詳解
這篇文章主要介紹了如何求兩個(gè)矩陣的乘積,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11