C++STL容器——string成員函數大全(超詳細)

来源:https://www.cnblogs.com/lynx-peng/archive/2022/08/05/16552710.html
-Advertisement-
Play Games

一、string 成員函數大全 構造 string()//構造空字元串 string(const char* s);//拷貝s所指向的字元串序列 string(const char* s, size_t n);//拷貝s所指向的字元串序列的第n個到結尾的字元 string(size_t n, cha ...


一、string 成員函數大全

構造

string()//構造空字元串
string(const char* s);//拷貝s所指向的字元串序列
string(const char* s, size_t n);//拷貝s所指向的字元串序列的第n個到結尾的字元
string(size_t n, char c);//將字元c複製n次
string(const string& str);//拷貝構造函數
string(const string& str, size_t pos, size_t len = npos);//拷貝s中從pos位置起的len個字元,若npos>字元串長度,就拷貝到字元串結尾結束

析構

~string();//刪除字元串

迭代器

/*迭代器*/
iterator begin(); //返回指向字元串第一個字元的迭代器
iterator end(); //返回指向字元串最後一個字元的下一個位置的迭代器
reverse_iterator rbegin(); //返回字元串最後一個字元的反向迭代器
reverse_iterator rend(); //返回指向字元串第一個字元之前的反向迭代器
/*常量迭代器*/
iterator cbegin(); //返回指向字元串第一個字元的迭代器
iterator cend(); //返回指向字元串最後一個字元的下一個位置的迭代器
reverse_iterator rcbegin(); //返回字元串最後一個字元的反向迭代器
reverse_iterator rcend(); //返回指向字元串第一個字元之前的反向迭代器

訪問

reference at(size_type pos);//同char& operator[],返回pos位置字元的引用,字元串可讀可寫
char& back();//返回最後字元的引用
char& front();//返回第一個字元的引用

長度及容量

size_t size();//返回字元串字元個數
size_t length();//返回字元串字元個數
size_t max_size();//返回string對象中可存放的最大字元串的長度
size_t capacity();//返回string分配的存儲容量。
void resize (size_t n);//調整源字元串的長度為n。
void resize (size_t n, char c);//調整字元串長度為n,並用字元c填充不足的部分
void reserve (size_t n = 0);//重新給源字元串分配存儲最小為n的容量
void shrink_to_fit()//清理記憶體,使字元串的容量變得等於字元串的大小
void clear();//將字元串的內容清空,讓源字元串成為一個空字元串(長度為0個字元)
bool empty();//判斷字元串是否為空

修飾

1. append() 在結尾添加字元串

string & append(const string & str)//在結尾添加一個string字元串
string & append(const string & str, size_type subpos, size_type sublen)//追加str中從subpos開始的sublen個字元(子串)
string & append(const charT * s)//C語言字元串
string & append(const charT * s, size_type n)//C語言字元串(長度為n的子串)
string & append(size_type n, charT c)//n個字元c
string & append(InputIterator first, InputIterator last)//使用迭代器append

2. push_back 將字元添加到串尾

void push_back (charT c);//將字元C添加到結尾

3. assign 賦值

string &assign(const char *s);///char*類型的字元串賦給當前字元串
string &assign(const char *s,int n);//用c字元串s開始的n個字元賦值
string &assign(const string &s);//把字元串s賦給當前字元串
string &assign(int n,char c);//用n個字元c賦值給當前字元串
string &assign(const string &s,int start,int n);//把字元串s中從start開始的n個字元賦給當前字元串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之間的部分賦給字元串

4. insert 在串中間插入

string & insert(size_type pos, const string & str)//在pos位置插入字元串str
string & insert(size_type pos, const string & str,size_type subpos, size_type sublen)//從subpos開始的sublen的子串
string & insert(size_type pos, const charT * s)//C語言字元串
string & insert(size_type pos, const charT * s, size_type n)//C語言字元串(長度為n的子串)
string & insert(size_type pos,   size_type n, charT c)//n個字元c
iterator insert(const_iterator p, size_type n, charT c)//使用迭代器索引插入n和字元
iterator insert(const_iterator p, charT c)//單一字元
iterator insert(iterator p, InputIterator first, InputIterator last)//使用迭代器insert

5. erase 刪除字元串中的特定字元

string & erase(size_type pos=0, size_type len=npos)//從pos處刪除len長度的字元
iterator erase(const_iterator p)//刪除迭代器所指的單一字元
iterator erase(const_iterator first, const_iterator last)//刪除2迭代器中間的字元

6. replace 替換字元串的一部分

string & replace(size_type pos,size_type len,const string & str)//從pos位置開始,長度為len的字元替換為str
string & replace(const_iterator i1, const_iterator i2, const string & str)//替換兩迭代器之間的字元
string & replace(size_type pos, size_type len, const string & str,size_type subpos, size_type sublen)//使用子串替換
string & replace(size_type pos,     size_type len,     const charT * s)//使用C語言字元串
string & replace(const_iterator i1, const_iterator i2, const charT * s)//迭代器方法
string & replace(size_type pos,     size_type len,     const charT * s, size_type n)//使用子串
string & replace(const_iterator i1, const_iterator i2, const charT * s, size_type n)//迭代器方法
string & replace(size_type pos,     size_type len,     size_type n, charT c)//用n個字元c替換
string & replace(const_iterator i1, const_iterator i2, size_type n, charT c)//迭代器方法
string & replace(const_iterator i1, const_iterator i2,
                       InputIterator first, InputIterator last)//迭代器方法

7. swap 交換2字元串

void swap (& str);//交換self和str

8. pop_back 刪除最後一個字元

void pop_back();//刪除串中最後一個字元

操作

1. C_str 轉為C語言字元串

const charT* c_str() const;//返回C語言字元串常量,指向以空字元終止的數組

2. data 轉為C語言字元數組

const charT* data() const;//返回C語言字元串常量,結尾沒有'\0'

3. get_allocator 用於分配記憶體的記憶體塊

allocator_type get_allocator() const;//它返回與容器關聯的分配器對象的副本。yongy

4.copy 複製到字元數組

size_type copy (charT* s, size_type len, size_type pos = 0) const;//從string類型對象中至多複製n個字元到字元指針p指向的空間中。不添加'\0'

5. find 查找

size_type find(const string & str, size_type pos=0) const;//從母字元串的pos位置查找字串str.存在返回字串第一個字元的位置,不存在返回npos
size_type find(const charT * s, size_type pos=0) const;//C語言字元串作為子串
size_type find(const charT * s, size_type pos, size_type n) const;//C語言字元串的子串(長度為n)作為被查找子串
size_type find(charT c, size_type pos=0) const;//查找單個字元
//倒著找
size_type rfind(const string & str, size_type pos=npos) const;
size_type rfind(const charT * s, size_type pos=npos);
size_type rfind(const charT * s, size_type pos, size_type n);
size_type rfind(charT c, size_type pos=npos) const;
//查找字元串中與目標字元(串中任一個字元)相同的第一個字元
size_type find_first_of(const string & str, size_type pos=0) const;
size_type find_first_of(const charT * s, size_type pos=0) const;
size_type find_first_of(const charT * s, size_type pos, size_type n);
size_type find_first_of(charT c, size_type pos=0) const;
//倒著找 or 找最後一個
size_type find_last_of(const string & str, size_type pos=npos) const 
size_type find_last_of(const charT * s, size_type pos=npos) const
size_type find_last_of(const charT * s, size_type pos, size_type n) const
size_type find_last_of(charT c, size_type pos=npos) const
//查找字元串中與目標字元(串中任一個字元)不相同的第一個字元
size_type find_first_not_of(const string & str, size_type pos=0) const;
size_type find_first_not_of(const charT * s, size_type pos=0) const;
size_type find_first_not_of(const charT * s, size_type pos, size_type n) const;
size_type find_first_not_of(charT c, size_type pos=0) const;
//倒著找 or 找最後一個
size_type find_last_not_of(const string & str, size_type pos=npos) const ;
size_type find_last_not_of(const charT * s, size_type pos=npos) const;
size_type find_last_not_of(const charT * s, size_type pos, size_type n) const;
size_type find_last_not_of(charT c, size_type pos=npos) const ;

6. substr 子串

string substr (size_type pos = 0, size_type len = npos) const;//返回一個從pos開始,len長度的string類型的子串

7.compare 比較

int compare (const string& str) const ;//比較字元串大小,源串大於str返回值>0,相同=0,小於<0
int compare (size_type pos, size_type len, const string& str) const;//用自身的子串比較
int compare (size_type pos, size_type len, const string& str,
             size_type subpos, size_type sublen) const;//兩字元串均為子串
int compare (const charT* s) const;//C語言字元串
int compare (size_type pos, size_type len, const charT* s) const;//C語言字元串子串
int compare (size_type pos, size_type len, const charT* s, size_type n) const;//雙子串

二、符號重載

1. = 賦值

string &operator=(const string &s);//把字元串s賦給當前字元串
string &operator=(const char* s);//char*類型的字元串賦給當前字元串
string &operator=(char c);//單個字元賦給當前字元串

2. [] 訪問

char& operator[] (size_t pos);//返回pos處字元的引用 越界導致未定義行為

3. += 追加

string& operator+= (const string& str);//在結尾加如str字元串,等效於append
string& operator+= (const char* s);//C語言字元串
string& operator+= (char c);//單個字元

三、非成員函數重載

函數/操作 作用
+ 合併兩個字元串
==,>=,<=,!=,>,< 比較字元串大小
>>,<< 加入輸入輸出流,用於輸入/輸出
getline 讀取一行,存儲在字元串中

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1 模板插值語法 在script 聲明一個變數可以直接在template 使用用法為{{變數名稱}} 模板語法是可以編寫條件運算的 運算也是支持的 操作API 也是支持的 <template> {{ message }} {{ message2==0 ? '我是老大' : '我笑的' }} {{ m ...
  • 在實際開發工作中,經常會碰到當select下拉數據過需要做分頁的情況 這裡簡單介紹封裝的一個Pagination-Select組件幾個步驟 封裝的比較簡易,可以根據自己的項目進行改動 /components/Pagination-Select/index.vue <template> <div id ...
  • AG Grid是一個客戶端 JavaScript網格 旨在與框架無關 它不依賴於任何框架 因此可以輕鬆地與任何框架集成 AG Grid支持具有相同API的多個框架 通過為每個框架量身定製的GUI層 獲得更好的開發人員體驗和性能 提供Community及Enterprise兩個版本 其中Enterpr ...
  • 本文將介紹一種巧用 background 配合 backdrop- filter 來構建有趣的透視背景效果的方式。 本技巧源自於一名群友的提問,如何構建如 ElementUI 文檔的一種頂欄背景特效,看看效果: 仔細看,在頁面的的滾動過程中,頂欄的背景不是白色的,也不是毛玻璃效果,而是能夠將背景顆粒 ...
  • ​ sass 運算符雖然沒有像那些編程語言那麼強大,但為了更靈活的輸出css,也增強了一些運算符的功能,例如賦值運算符、等號操作符、比較運算符、邏輯運算符、字元串運算符...等等,接下來就來詳細介紹下這些運算符的基本使用 賦值運算符 賦值運算符就是把一個值賦值給一個變數,通過冒號(:)的方式進行承接 ...
  • 1. 事件起因 最近在做一個關於星座的移動端項目,想實現這樣一個需求,每次切換導航欄NavBar item時,都會使下麵的頁面級組件TodayView更改背景色樣式(如圖1到圖2,導航欄從雙魚座切換到處女座,下麵頁面級組件的背景顏色由黃色切換至粉色)。 圖1 圖2 如果利用傳統的辦法,在點擊事件的事 ...
  • 面向對象是一種軟體開發的編程範式。其概念和應用已超越了程式設計和軟體開發,擴展到如資料庫系統、互動式界面、應用結構、應用平臺、分散式系統、網路管理結構、CAD 技術、人工智慧等領域。 ...
  • 三、項目實現讀寫分離 實現方式跟同一個目錄下的01-讀寫分離測試案例基本一致,只不過是將資料庫替換成了項目使用的資料庫 ==同時還有非常重要的一點,ShardingSphere-JDBC的作用不止是讀寫分離,更重要的是其能通過配置文件配置指定演算法,可以自動化的完成對資料庫進行分庫分表操作,且不需要更 ...
一周排行
    -Advertisement-
    Play Games
  • GoF之工廠模式 @目錄GoF之工廠模式每博一文案1. 簡單說明“23種設計模式”1.2 介紹工廠模式的三種形態1.3 簡單工廠模式(靜態工廠模式)1.3.1 簡單工廠模式的優缺點:1.4 工廠方法模式1.4.1 工廠方法模式的優缺點:1.5 抽象工廠模式1.6 抽象工廠模式的優缺點:2. 總結:3 ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 本章將和大家分享ES的數據同步方案和ES集群相關知識。廢話不多說,下麵我們直接進入主題。 一、ES數據同步 1、數據同步問題 Elasticsearch中的酒店數據來自於mysql資料庫,因此mysql數據發生改變時,Elasticsearch也必須跟著改變,這個就是Elasticsearch與my ...
  • 引言 在我們之前的文章中介紹過使用Bogus生成模擬測試數據,今天來講解一下功能更加強大自動生成測試數據的工具的庫"AutoFixture"。 什麼是AutoFixture? AutoFixture 是一個針對 .NET 的開源庫,旨在最大程度地減少單元測試中的“安排(Arrange)”階段,以提高 ...
  • 經過前面幾個部分學習,相信學過的同學已經能夠掌握 .NET Emit 這種中間語言,並能使得它來編寫一些應用,以提高程式的性能。隨著 IL 指令篇的結束,本系列也已經接近尾聲,在這接近結束的最後,會提供幾個可供直接使用的示例,以供大伙分析或使用在項目中。 ...
  • 當從不同來源導入Excel數據時,可能存在重覆的記錄。為了確保數據的準確性,通常需要刪除這些重覆的行。手動查找並刪除可能會非常耗費時間,而通過編程腳本則可以實現在短時間內處理大量數據。本文將提供一個使用C# 快速查找並刪除Excel重覆項的免費解決方案。 以下是實現步驟: 1. 首先安裝免費.NET ...
  • C++ 異常處理 C++ 異常處理機制允許程式在運行時處理錯誤或意外情況。它提供了捕獲和處理錯誤的一種結構化方式,使程式更加健壯和可靠。 異常處理的基本概念: 異常: 程式在運行時發生的錯誤或意外情況。 拋出異常: 使用 throw 關鍵字將異常傳遞給調用堆棧。 捕獲異常: 使用 try-catch ...
  • 優秀且經驗豐富的Java開發人員的特征之一是對API的廣泛瞭解,包括JDK和第三方庫。 我花了很多時間來學習API,尤其是在閱讀了Effective Java 3rd Edition之後 ,Joshua Bloch建議在Java 3rd Edition中使用現有的API進行開發,而不是為常見的東西編 ...
  • 框架 · 使用laravel框架,原因:tp的框架路由和orm沒有laravel好用 · 使用強制路由,方便介面多時,分多版本,分文件夾等操作 介面 · 介面開發註意欄位類型,欄位是int,查詢成功失敗都要返回int(對接java等強類型語言方便) · 查詢介面用GET、其他用POST 代碼 · 所 ...
  • 正文 下午找企業的人去鎮上做貸後。 車上聽同事跟那個司機對罵,火星子都快出來了。司機跟那同事更熟一些,連我在內一共就三個人,同事那一手指桑罵槐給我都聽愣了。司機也是老社會人了,馬上聽出來了,為那個無辜的企業經辦人辯護,實際上是為自己辯護。 “這個事情你不能怪企業。”“但他們總不能讓銀行的人全權負責, ...