企業(yè)單頁網(wǎng)站模板推廣普通話奮進(jìn)新征程
【小夢(mèng)C嘎嘎——啟航篇】string常用接口的模擬實(shí)現(xiàn)😎
- 前言🙌
- string 模擬實(shí)現(xiàn)
- 1、iterator 迭代器相關(guān)使用函數(shù)實(shí)現(xiàn)
- 2、構(gòu)造函數(shù)接口實(shí)現(xiàn)
- 3、 傳統(tǒng)寫法——拷貝構(gòu)造函數(shù)接口實(shí)現(xiàn)
- 4、 現(xiàn)代寫法——拷貝構(gòu)造函數(shù)接口實(shí)現(xiàn)
- 5、析構(gòu)函數(shù)接口實(shí)現(xiàn)
- 6、傳統(tǒng)寫法—— = 賦值運(yùn)算符重載函數(shù)接口實(shí)現(xiàn)
- 7、現(xiàn)代寫法—— = 賦值運(yùn)算符重載函數(shù)接口實(shí)現(xiàn)
- 8 、極致現(xiàn)代寫法—— = 賦值運(yùn)算符重載函數(shù)接口實(shí)現(xiàn)
- 9、swap函數(shù)接口實(shí)現(xiàn)
- 10、【】下標(biāo)訪問函數(shù)接口實(shí)現(xiàn)
- 11、容量、大小數(shù)據(jù)獲取函數(shù)接口實(shí)現(xiàn)
- 12、C語言格式函數(shù)字符串獲取函數(shù)接口實(shí)現(xiàn)
- 13、擴(kuò)容函數(shù)reserve實(shí)現(xiàn)接口
- 14、resize函數(shù)(實(shí)現(xiàn)擴(kuò)容,改變size,初始化)實(shí)現(xiàn)接口
- 15、find函數(shù)接口實(shí)現(xiàn)
- 16、substr 取子串函數(shù)接口實(shí)現(xiàn)
- 17、尾插函數(shù)接口實(shí)現(xiàn)
- 18、append函數(shù)(尾插字符串)接口實(shí)現(xiàn)
- 19、+= 運(yùn)算符重載函數(shù)實(shí)現(xiàn)
- 20、任意位置插入字符/字符串函數(shù)接口實(shí)現(xiàn)
- 21、任意位置刪除len個(gè)字符
- 22、字符串大小比較運(yùn)算符重載函數(shù)實(shí)現(xiàn)
- 23、清空函數(shù)clear函數(shù)接口實(shí)現(xiàn)
- 24、流插入運(yùn)算符重載函數(shù)接口實(shí)現(xiàn)
- 25、流提取運(yùn)算符重載函數(shù)接口實(shí)現(xiàn)
- 判空函數(shù)接口實(shí)現(xiàn)
- 總結(jié)撒花💞

? ?
😎博客昵稱:博客小夢(mèng)
😊最喜歡的座右銘:全神貫注的上吧!!!
😊作者簡(jiǎn)介:一名熱愛C/C++,算法等技術(shù)、喜愛運(yùn)動(dòng)、熱愛K歌、敢于追夢(mèng)的小博主!
😘博主小留言:哈嘍!😄各位CSDN的uu們,我是你的博客好友小夢(mèng),希望我的文章可以給您帶來一定的幫助,話不多說,文章推上!歡迎大家在評(píng)論區(qū)嘮嗑指正,覺得好的話別忘了一鍵三連哦!😘
前言🙌
? ? 哈嘍各位友友們😊,我今天又學(xué)到了很多有趣的知識(shí),現(xiàn)在迫不及待的想和大家分享一下!都是精華內(nèi)容,可不要錯(cuò)過喲!!!😍😍😍
string 模擬實(shí)現(xiàn)
1、iterator 迭代器相關(guān)使用函數(shù)實(shí)現(xiàn)
iterator begin(){return _str;}terator end(){return _str + _size;}//針對(duì)const對(duì)象設(shè)計(jì)的版本const_iterator begin() const{return _str;}const_iterator end() const{return _str + _size;}
2、構(gòu)造函數(shù)接口實(shí)現(xiàn)
tring(const char* str = ""):_size(strlen(str)), _capacity(_size){_str = new char[_capacity + 1];strcpy(_str, str);}
3、 傳統(tǒng)寫法——拷貝構(gòu)造函數(shù)接口實(shí)現(xiàn)
// 傳統(tǒng)寫法
//s2(s1)string(const string& s):_str(nullptr),_size(0),_capacity(0){_str = new char[s._capacity + 1];strcpy(_str, s._str);_capacity = s._capacity;_size = s._size;}
4、 現(xiàn)代寫法——拷貝構(gòu)造函數(shù)接口實(shí)現(xiàn)
// s2(s1)string(const string& s):_str(nullptr), _size(0), _capacity(0){string tmp(s._str);swap(tmp);}
5、析構(gòu)函數(shù)接口實(shí)現(xiàn)
~string(){delete[] _str;_str = nullptr;_size = _capacity = 0;}
6、傳統(tǒng)寫法—— = 賦值運(yùn)算符重載函數(shù)接口實(shí)現(xiàn)
// s2 = s3string& operator=(const string& s){if (this != &s){char* tmp = new char[s._capacity + 1];strcpy(tmp, s._str);delete[] _str;_str = tmp;_size = s._size;_capacity = s._capacity;}return *this;}
7、現(xiàn)代寫法—— = 賦值運(yùn)算符重載函數(shù)接口實(shí)現(xiàn)
// s2 = s3string& operator=(const string& s){if (this != &s){string tmp(s);//this->swap(tmp);swap(tmp);}return *this;}
8 、極致現(xiàn)代寫法—— = 賦值運(yùn)算符重載函數(shù)接口實(shí)現(xiàn)
// s2 = s3string& operator=(string tmp){swap(tmp);return *this;}
9、swap函數(shù)接口實(shí)現(xiàn)
void swap(string& s){std::swap(_str, s._str);std::swap(_size, s._size);std::swap(_capacity, s._capacity);}
10、【】下標(biāo)訪問函數(shù)接口實(shí)現(xiàn)
char& operator[](size_t pos){assert(pos < _size);return _str[pos];}const char& operator[](size_t pos) const{assert(pos < _size);return _str[pos];}
11、容量、大小數(shù)據(jù)獲取函數(shù)接口實(shí)現(xiàn)
size_t capacity() const{return _capacity;}size_t size() const{return _size;}
12、C語言格式函數(shù)字符串獲取函數(shù)接口實(shí)現(xiàn)
const char* c_str() const{return _str;}
13、擴(kuò)容函數(shù)reserve實(shí)現(xiàn)接口
void reserve(size_t n){if (n > _capacity){char* tmp = new char[n + 1];strcpy(tmp, _str);delete[] _str;_str = tmp;_capacity = n;}}
14、resize函數(shù)(實(shí)現(xiàn)擴(kuò)容,改變size,初始化)實(shí)現(xiàn)接口
void resize(size_t n, char ch = '\0'){if (n <= _size){_str[n] = '\0';_size = n;}else{reserve(n);while (_size < n){_str[_size] = ch;++_size;}_str[_size] = '\0';}}
15、find函數(shù)接口實(shí)現(xiàn)
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;}else{return npos;}}
16、substr 取子串函數(shù)接口實(shí)現(xiàn)
string substr(size_t pos, size_t len = npos){string s;size_t end = pos + len;if (len == npos || pos + len >= _size) // 有多少取多少{len = _size - pos;end = _size;}s.reserve(len);for (size_t i = pos; i < end; i++){s += _str[i];}return s;}
17、尾插函數(shù)接口實(shí)現(xiàn)
void push_back(char ch){if (_size == _capacity){reserve(_capacity == 0 ? 4 : _capacity * 2);}_str[_size] = ch;++_size;_str[_size] = '\0';}
18、append函數(shù)(尾插字符串)接口實(shí)現(xiàn)
void append(const char* str){size_t len = strlen(str);if (_size + len > _capacity){reserve(_size + len);}strcpy(_str + _size, str);_size += len;}
19、+= 運(yùn)算符重載函數(shù)實(shí)現(xiàn)
string& operator+=(char ch){push_back(ch);return *this;}string& operator+=(const char* str){append(str);return *this;}
20、任意位置插入字符/字符串函數(shù)接口實(shí)現(xiàn)
// insert(0, 'x')void insert(size_t pos, char ch){assert(pos <= _size);if (_size == _capacity){reserve(_capacity == 0 ? 4 : _capacity * 2);}// 17:17size_t end = _size + 1;while (end > pos){_str[end] = _str[end - 1];--end;}_str[pos] = ch;_size++;}//任意位置插入一個(gè)字符串void insert(size_t pos, const char* str){assert(pos <= _size);size_t len = strlen(str);if (_size + len > _capacity){reserve(_size + len);}// 挪動(dòng)數(shù)據(jù)int end = _size;while (end >= (int)pos){_str[end + len] = _str[end];--end;}strncpy(_str + pos, str, len);_size += len;}
21、任意位置刪除len個(gè)字符
void erase(size_t pos, size_t len = npos){assert(pos < _size);if (len == npos || pos + len >= _size){_str[pos] = '\0';_size = pos;}else{size_t begin = pos + len;while (begin <= _size){_str[begin - len] = _str[begin];++begin;}_size -= len;}}
22、字符串大小比較運(yùn)算符重載函數(shù)實(shí)現(xiàn)
bool operator<(const string& s) const{return strcmp(_str, s._str) < 0;}bool operator==(const string& s) const{return strcmp(_str, s._str) == 0;}bool operator<=(const string& s) const{return *this < s || *this == s;}bool operator>(const string& s) const{return !(*this <= s);}bool operator>=(const string& s) const{return !(*this < s);}bool operator!=(const string& s) const{return !(*this == s);}
23、清空函數(shù)clear函數(shù)接口實(shí)現(xiàn)
void clear(){_str[0] = '\0';_size = 0;}
24、流插入運(yùn)算符重載函數(shù)接口實(shí)現(xiàn)
ostream& operator<<(ostream& out, const string& s){/*for (size_t i = 0; i < s.size(); i++){out << s[i];}*/for (auto ch : s)out << ch;return out;}
25、流提取運(yùn)算符重載函數(shù)接口實(shí)現(xiàn)
istream& operator>>(istream& in, string& s){// 17:15繼續(xù)s.clear();//s.reserve(128);char buff[129];size_t i = 0;char ch;ch = in.get();while (ch != ' ' && ch != '\n'){buff[i++] = ch;if (i == 128){buff[i] = '\0';s += buff;i = 0;}//s += ch;ch = in.get();}if (i != 0){buff[i] = '\0';s += buff;}return in;}
判空函數(shù)接口實(shí)現(xiàn)
bool empty()const{return _size == 0;}
總結(jié)撒花💞
? ?希望大家通過閱讀此文有所收獲!
? ?😘如果我寫的有什么不好之處,請(qǐng)?jiān)谖恼孪路浇o出你寶貴的意見😊。如果覺得我寫的好的話請(qǐng)點(diǎn)個(gè)贊贊和關(guān)注哦~😘😘😘