string的模擬實現 (6)

目錄

1.string.h

2.string.cpp

3.test.cpp

4.一些注意點


本篇博客就學習下如何模擬實現簡易版的string類,學好string類后面學習其他容器也會更輕松些。

代碼實現如下:

1.string.h

#define  _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <iostream>
#include <assert.h>
#include <string>
using namespace std;
namespace lwx {class string {private:char* _str=nullptr;size_t _size=0;size_t _capacity=0;public:using iterator = char*;using const_iterator = const char*;string(const char* s = "");string(const string& str);string& operator=(const string& str);~string();size_t size()const {return _size;}size_t capacity()const {return _capacity;}char* c_str()const {return _str;}void clear() {_str[0] = '\0';_size = 0;}string substr(size_t pos, size_t len = npos)const;iterator begin() {return _str;}iterator end() {return _str + _size;}const_iterator begin() const {return _str;}const_iterator end()const {return _str + _size;}string& operator+=(char c);string& operator+=(const char* s);void push_back(char c);void append(const char* s);void erase(size_t pos, size_t len = npos);void insert(size_t pos, char c);void insert(size_t pos, const char* s);size_t find(char c, size_t pos=0)const;size_t find(const char* s, size_t pos=0)const;void reserve(size_t n) {if (n > _capacity) {char* p = new char[n + 1];strcpy(p, _str);delete[] _str;_str = p;_capacity = n;}}char& operator[](size_t n) {assert(n <= _size);return _str[n];}const char& operator[](size_t n)const {assert(n <= _size);return _str[n];}public:static const size_t npos;};bool operator==(const string& st, const string& str);bool operator!=(const string& st, const string& str);bool operator>=(const string& st, const string& str);bool operator<=(const string& st, const string& str);bool operator>(const string& st, const string& str);bool operator<(const string& st, const string& str);ostream& operator<<(ostream& os, const string& str);istream& operator>>(istream& is, string& str);
}

2.string.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include"string.h"
namespace lwx {const size_t string::npos = -1;string::string(const char* s):_size(strlen(s)){_capacity = _size;_str = new char[_capacity + 1];strcpy(_str, s);}string::string(const string& str) {_size = str._size;_capacity = str._capacity;_str = new char[_capacity + 1];strcpy(_str, str._str);}string& string::operator=(const string& str) {if (*this != str) {delete[] _str;_size = str._size;_capacity = str._capacity;_str = new char[_capacity+1];strcpy(_str, str._str);}return *this;}string::~string() {delete[] _str;_str = nullptr;_size = 0;_capacity = 0;}void string::push_back(char c) {if (_size == _capacity) {reserve(_capacity==0?4:2*_capacity);}_str[_size] = c;_str[_size + 1] = '\0';_size++;}void string::append(const char* s) {if (_size + strlen(s) > _capacity) {reserve(2 * _capacity);}if (_size + strlen(s) > _capacity)reserve(_size + strlen(s));strcpy(_str+_size, s);_size += strlen(s);}string& string::operator+=(char c) {push_back(c);return *this;}string& string::operator+=(const char* s) {append(s);return *this;}string string::substr(size_t pos,size_t len) const{assert(pos < _size);if (len>_size-pos) {len = _size - pos;}lwx::string sub;sub.reserve(len+1);for (size_t i = 0; i < len; i++) {sub._str[i] = _str[pos + i];}return sub;}void string::erase(size_t pos, size_t len ) {assert(pos <= _size);if (len >= _size - pos) {_str[pos] = '\0';_size = pos;}else {for (size_t i = 0; i <len; i++) {_str[pos+i] = _str[pos + len+i];}}_size -= len;}void string::insert(size_t pos, char c) {assert(pos <= _size);if (_size == _capacity)reserve(_capacity==0?4:2 * _capacity);size_t end=_size+1;while (end > pos) {_str[end] = _str[end - 1];end--;}_str[pos] = c;_size++;}void string::insert(size_t pos, const char* s) {assert(pos <= _size);size_t n = strlen(s);if (_size + n > _capacity)reserve(2 * _capacity);if (_size + n > _capacity)reserve(_size + n);size_t end = _size + n;while (n > 0) {_str[end + n] = _str[end];n--;}for (size_t i = 0; i < n; i++) {_str[pos + i] = s[i];}_size += n;}size_t string::find(char c, size_t pos) const{assert(pos < _size);for (size_t i = pos; i < _size; i++) {if (_str[i] == c)return i;}return npos;}size_t string::find(const char* s, size_t pos) const{assert(pos < _size);const char* p = strstr(_str + pos, s);if (p == nullptr) {return npos;}else {return p - _str;}return npos;}bool operator==(const string& st, const string& str) {return strcmp(st.c_str(), str.c_str()) == 0;}bool operator!=(const string& st, const string& str) {return !(st == str);}bool operator>=(const string& st, const string& str) {return !(st < str);}bool operator<=(const string& st, const string& str) {return !(st > str);}bool operator>(const string& st, const string& str) {return strcmp(st.c_str(), str.c_str()) > 0;}bool operator<(const string& st, const string& str) {return !(st > str || st == str);}ostream& operator<<(ostream& os,const string& str) {for (size_t i = 0; i < str.size(); i++) {os << str[i];}return os;}istream& operator>>(istream& is, string& str) {str.clear();char c;c = is.get();while (c != ' ' && c != '\n') {str += c;c = is.get();}return is;}
}

3.test.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include"string.h"void test_string1()
{lwx::string s2;cout << s2.c_str() << endl;lwx::string s1("hello world");cout << s1.c_str() << endl;s1[0] = 'x';cout << s1.c_str() << endl;for (size_t i = 0; i < s1.size(); i++){cout << s1[i] << " ";}cout << endl;// 迭代器 -- 像指針一樣的對象lwx::string::iterator it1 = s1.begin();while (it1 != s1.end()){(*it1)--;++it1;}cout << endl;it1 = s1.begin();while (it1 != s1.end()){cout << *it1 << " ";++it1;}cout << endl;// 修改// 底層是迭代器的支持// 意味著支持迭代器就支持范圍forfor (auto& ch : s1){ch++;}for (auto ch : s1){cout << ch << " ";}cout << endl;const lwx::string s3("xxxxxxxxx");for (auto& ch : s3){//ch++;cout << ch << " ";}cout << endl;
}void test_string2()
{lwx::string s1("hello world");cout << s1.c_str() << endl;s1 += '#';s1 += "#hello world";cout << s1.c_str() << endl;lwx::string s2("hello world");cout << s2.c_str() << endl;s2.insert(6, 'x');cout << s2.c_str() << endl;s2.insert(0, 'x');cout << s2.c_str() << endl;lwx::string s3("hello world");cout << s3.c_str() << endl;s3.insert(6, "xxx");cout << s3.c_str() << endl;s3.insert(0, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");cout << s3.c_str() << endl;
}void test_string3()
{lwx::string s1("hello world");cout << s1.c_str() << endl;s1.erase(6, 2);cout << s1.c_str() << endl;s1.erase(5, 20);cout << s1.c_str() << endl;s1.erase(3);cout << s1.c_str() << endl;
}void test_string4()
{lwx::string s1("hello world");cout << s1.find(' ') << endl;cout << s1.find("wo") << endl;lwx::string s2 = "https://legacy.cplusplus.com/reference/cstring/strstr/?kw=strstr";//bit::string s2 = "https://blog.csdn.net/ww753951/article/details/130427526";size_t pos1 = s2.find(':');size_t pos2 = s2.find('/', pos1 + 3);if (pos1 != string::npos && pos2 != string::npos){lwx::string domain = s2.substr(pos1 + 3, pos2 - (pos1 + 3));cout << domain.c_str() << endl;lwx::string uri = s2.substr(pos2 + 1);cout << uri.c_str() << endl;}
}void test_string5()
{lwx::string s1("hello world");lwx::string s2(s1);cout << s1.c_str() << endl;cout << s2.c_str() << endl;s1[0] = 'x';cout << s1.c_str() << endl;cout << s2.c_str() << endl;lwx::string s3("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");s1 = s3;cout << s1.c_str() << endl;cout << s3.c_str() << endl;s1 = s1;cout << s1.c_str() << endl;
}void test_string6()
{lwx::string s1("hello world");lwx::string s2(s1);lwx::string s3 = s1;// 構造+拷貝 ->優化直接構造lwx::string s4 = "hello world";cout << (s1 == s2) << endl;cout << (s1 < s2) << endl;cout << (s1 > s2) << endl;cout << (s1 == "hello world") << endl;cout << ("hello world" == s1) << endl;//operator<<(cout, s1); cout << s1 << endl;cin >> s1;cout << s1 << endl;std::string ss1("hello world");cin >> ss1;cout << ss1 << endl;
}int main()
{test_string6();return 0;
}

4.一些注意點

①我們定義的string類會和庫里面有沖突的風險,所以我們可以用命名空間namespace進行封裝。

②編寫默認構造函數時,我們不能給_str(nullptr)缺省值,因為cout<<(const char*)_str<<endl不會當指針打印,它會自動識別類型,覺得你是想打印字符串,而打印字符串遇到'\0'才會終止,但_str為空指針,這就有解引用空指針問題了。

但標準庫里的string不會有這種問題,解決方法:直接給'\0'開一個空間就行了

③在string這里申請空間要多給一個空間用來存放'\0',,但capacity不將'\0'計算其中,空間真實大小=_capacity+1。

④在前面類和對象下我們說到,盡可能的使用初始化列表,但在這里使用比較別捏,三個strlen(),strlen是在運行時計算的,3個O(n),還是很坑的。

那我們改一下,下面這個比上面的運行效率時好多了,但是這種寫法是錯的。因為我們之前說過,初始化列表會按聲明的順序初始化,先走_str,再走_size,所以我們還得把聲明順序變了才行,但聲明順序變了又不順我們的習慣(先聲明指針),而且過后還要把數據拷貝出來,所以說我們是得盡可能使用初始化列表,但有些東西是初始化列表搞不定的,改該用函數體還是得用,不能說有了初始化列表就不用函數體了。

最終我們可以改成,這樣就不用管順序了。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/77704.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/77704.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/77704.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Unity:像素(Pixels) 和 單位(Units)

目錄 從第一性原理出發&#xff1a;什么是像素和 Unit&#xff1f; &#x1f9f1; 1. 像素&#xff08;Pixel&#xff09;&#xff1a;圖像的最小單位 &#x1f4d0; 2. Unity Unit&#xff08;單位&#xff09;&#xff1a;游戲世界中的度量單位 核心換算公式&#xff1a;…

【失敗總結】Win10系統安裝docker

1.啟用或關閉windows功能中&#xff0c;將Hyper-V功能勾選全部啟用&#xff0c;容器勾選。設置好后要重啟電腦。 2.管網下載下載安裝Docker  Docker官網&#xff1a;https://www.docker.com/ 3.可以自定義Docker安裝路徑 新建安裝目錄&#xff1a;d:\MySoftware\Docker并將D…

《Adaptive Layer-skipping in Pre-trained LLMs》- 論文筆記

作者&#xff1a;Xuan Luo, Weizhi Wang, Xifeng Yan Department of Computer Science, UC Santa Barbara xuan_luoucsb.edu, weizhiwangucsb.edu, xyancs.ucsb.edu 1. 引言與動機 1.1 背景 LLM 的成功與挑戰: 大型語言模型 (LLMs) 在翻譯、代碼生成、推理等任務上取得巨大成…

DQN在Gym的MountainCar環境的實現

DQN on MountainCar 引言 在本次實驗里&#xff0c;我構建了DQN和Dueling DQN&#xff0c;并在Gymnasium庫的MountainCar環境中對它們展開測試。我通過調整訓練任務的超參數&#xff0c;同時設計不同的獎勵函數及其對應參數&#xff0c;致力于獲取更優的訓練效果。最后&#…

計算機網絡綜合實驗指南

計算機網絡綜合實驗指南 本實驗將結合《計算機網絡自頂向下》前三章的核心概念&#xff0c;通過實際操作加深對應用層、運輸層和網絡層的理解。實驗涵蓋 HTTP/TCP抓包分析、DNS解析觀察、網頁性能評估及簡單Socket編程&#xff0c;幫助你將理論轉化為實踐。 實驗準備 工具&…

【AI部署】騰訊云GPU-RUN—SadTalker的AI數字人視頻—未來之窗超算中心

磁盤空間 創建未來之窗 查看磁盤命令 df -h 指定路徑創建環境 conda create --prefix sadtalker python3.10 指令路徑運行環境 conda activate ./sadtalker 安裝環境 pip install torch1.12.1cu113 torchvision0.13.1cu113 torchaudio0.12.1 --extra-index-url https://…

爬蟲利器SpiderTools谷歌插件教程v1.0.0!!!web端JavaScript環境檢測!!!

SpiderTools谷歌插件教程v1.0.0 一、SpiderTools簡介二、下載通道三、插件介紹四、插件使用五、工具函數使用 補環境工具推薦&#xff1a;爬蟲補環境利器webEnv 一、SpiderTools簡介 SpiderTools主要用于檢測和監控網頁的JavaScript運行環境。該插件可以幫助開發者更好地查看…

Android開發協調布局滑動懸停

Android開發協調布局滑動懸停 直接給個xml,防止下次忘了怎么寫。 <?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"x…

Linux學習——TCP

一.TCP編程API 1.socket函數 1.socket函數 include include int socket(int domain,int type,int protocol); 參數 domain AF_INET AF_INET6 AF_UNIX,AF_LOCAL AF_NETLINK AF_PACKET type SOCK_STREAM: 流式…

Linux驅動開發--異步通知與異步I/O

3、異步通知與異步I/O 3.1 Linux信號 阻塞與非阻塞訪問、poll()函數提供了較好的解決設備訪問的機制&#xff0c;但是如果有了異步通知&#xff0c;整套機制則更加完整了。 異步通知的意思是&#xff1a;一旦設備就緒&#xff0c;則主動通知應用程序&#xff0c;這樣應用程序…

大語言模型推理能力的強化學習現狀理解GRPO與近期推理模型研究的新見解

每周跟蹤AI熱點新聞動向和震撼發展 想要探索生成式人工智能的前沿進展嗎&#xff1f;訂閱我們的簡報&#xff0c;深入解析最新的技術突破、實際應用案例和未來的趨勢。與全球數同行一同&#xff0c;從行業內部的深度分析和實用指南中受益。不要錯過這個機會&#xff0c;成為AI領…

【Linux系統】Linux基礎指令(詳解Linux命令行常用指令,每一個指令都有示例演示)

文章目錄 一、與文件路徑相關的指令0.補充知識&#xff1a;路徑的認識1.pwd 指令2.cd 指令&#xff08;含家目錄的介紹&#xff09; 二、創建和刪除文件的指令0.補充知識&#xff1a;普通文件和目錄文件1.touch 指令&#xff08;可以修改文件的時間戳&#xff09;2.mkdir 指令3…

LangChain 單智能體模式示例【純代碼】

# LangChain 單智能體模式示例import os from typing import Anyfrom langchain.agents import AgentType, initialize_agent, Tool from langchain_openai import ChatOpenAI from langchain.tools import BaseTool from langchain_experimental.tools.python.tool import Pyt…

解決:VSCode C++ conan 安裝第三方庫后 頭文件報錯

文章目錄 1 頭文件include路徑查找報錯參考 1 頭文件include路徑查找報錯 找到conan_toolchain.cmake中 INCLUDE_PATH list(PREPEND CMAKE_INCLUDE_PATH "/Users/hanliqiang/.conan2/p/b/fmte8c4f7a755477/p/include")生成C編譯配置 CtrlShiftP 中選擇C Edit Confi…

松靈Cobot Magic雙臂具身遙操機器人(基于ROS的定位建圖與協同導航技術)

摘要 本文以CobotMagic可移動協作機器人為研究對象&#xff0c;從硬件架構設計、軟件系統架構、多傳感器融合定位建圖系統、智能導航系統協同機制四個維度&#xff0c;深入解析機器人系統工作原理。重點研究多傳感器融合定位建圖系統實現原理&#xff0c;結合實測數據驗證系統…

回歸,git 分支開發操作命令

核心分支說明 主分支&#xff08;master/production&#xff09;存放隨時可部署到生產環境的穩定代碼&#xff0c;僅接受通過測試的合并請求。 開發分支&#xff08;develop&#xff09;集成所有功能開發的穩定版本&#xff0c;日常開發的基礎分支&#xff0c;從該分支創建特性…

ASP.NET Core 最小 API:極簡開發,高效構建(下)

在上篇文章 ASP.NET Core 最小 API&#xff1a;極簡開發&#xff0c;高效構建&#xff08;上&#xff09; 中我們添加了 API 代碼并且測試&#xff0c;本篇繼續補充相關內容。 一、使用 MapGroup API 示例應用代碼每次設置終結點時都會重復 todoitems URL 前綴。 API 通常具有…

Spring之我見 - Spring Boot Starter 自動裝配原理

歡迎光臨小站&#xff1a;致橡樹 Spring Boot Starter 的核心設計理念是 約定優于配置&#xff0c;其核心實現基于 自動配置&#xff08;Auto-Configuration&#xff09; 和 條件化注冊&#xff08;Conditional Registration&#xff09;。以下是其生效原理&#xff1a; 約定…

精益數據分析(7/126):打破創業幻想,擁抱數據驅動

精益數據分析&#xff08;7/126&#xff09;&#xff1a;打破創業幻想&#xff0c;擁抱數據驅動 在創業的道路上&#xff0c;我們都懷揣著夢想&#xff0c;但往往容易陷入自我編織的幻想中。我希望通過和大家一起學習《精益數據分析》&#xff0c;能幫助我們更清醒地認識創業過…

牛客java練習題

[toc] 1.依賴注入 依賴注入是一種設計模式和編程思想,不依賴 具體的框架實現,可以通過多種方式和框架來實現可以通過Spring , Google Guice , PicoContainer 等都可以實現依賴注入,也可以通過手動編寫實現目的: 為了解耦合,將對象之間的依賴關系從代碼中解耦出來, 使系統更加…