關於C++的STRING的成員函數彙總

来源:http://www.cnblogs.com/shouce/archive/2016/03/16/5282012.html
-Advertisement-
Play Games

string類的構造函數: string(const char *s); //用c字元串s初始化 string(int n,char c); //用n個字元c初始化 此外,string類還支持預設構造函數和複製構造函數,如string s1;string s2="hello";都是正確的寫法。當構造


string類的構造函數:

  string(const char *s);    //用c字元串s初始化
  string(int n,char c);      //用n個字元c初始化 
  此外,string類還支持預設構造函數和複製構造函數,如string s1;string s2="hello";都是正確的寫法。當構造的string太長而無法表達時會拋出length_error異常

string類的字元操作:

  const char &operator[](int n)const; 
  const char &at(int n)const; 
  char &operator[](int n); 
  char &at(int n); 
  以上函數operator[]和at()均返回當前字元串中第n個字元的位置,但at函數提供範圍檢查,當越界時會拋出out_of_range異常,下標運算符[]不提供檢查訪問。
  const char *data()const;                            //返回一個非null終止的c字元數組 
  const char *c_str()const;                           //返回一個以null終止的c字元串 
  int copy(char *s, int n, int pos = 0) const;   //把當前串中以pos開始的n個字元拷貝到以s為起始位置的字元數組中,返回實際拷貝的數目

string的特性描述:

  int capacity()const;                //返回當前容量(即string中不必增加記憶體即可存放的元素個數)
  int max_size()const;              //返回string對象中可存放的最大字元串的長度
  int size()const;                      //返回當前字元串的大小
  int length()const;                  //返回當前字元串的長度 
  bool empty()const;               //當前字元串是否為空
  void resize(int len,char c);     //把字元串當前大小置為len,並用字元c填充不足的部分

string類的輸入輸出操作:

  string類重載運算符operator>>用於輸入,同樣重載運算符operator<<用於輸出操作。 
  函數getline(istream &in,string &s);用於從輸入流in中讀取字元串到s中,以換行符'\n'分開。

string的賦值:

  string &operator=(const string &s);                            //把字元串s賦給當前字元串 
  string &assign(const char *s);                                    //用c類型字元串s賦值 
  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迭代器之間的部分賦給字元串

string的連接:

  string &operator+=(const string &s);                    //把字元串s連接到當前字元串的結尾 
  string &append(const char *s);                             //把c類型字元串s連接到當前字元串結尾 
  string &append(const char *s,int n);                     //把c類型字元串s的前n個字元連接到當前字元串結尾 
  string &append(const string &s);                          //同operator+=() 
  string &append(const string &s,int pos,int n);        //把字元串s中從pos開始的n個字元連接到當前字元串的結尾 
  string &append(int n,char c);                               //在當前字元串結尾添加n個字元c 
  string &append(const_iterator first,const_iterator last); //把迭代器first和last之間的部分連接到當前字元串的結尾

string的比較:

  bool operator==(const string &s1,const string &s2)const;             //比較兩個字元串是否相等 
  運算符">","<",">=","<=","!="均被重載用於字元串的比較; 
  int compare(const string &s) const;                                             //比較當前字元串和s的大小 
  int compare(int pos, int n,const string &s)const;                          //比較當前字元串從pos開始的n個字元組成的字元串與s的大小 
  int compare(int pos, int n,const string &s,int pos2,int n2)const;   //比較當前字元串從pos開始的n個字元組成的字元串與s中pos2開始的n2個字元組成的字元串的大小
  int compare(const char *s) const; 
  int compare(int pos, int n,const char *s) const;
  int compare(int pos, int n,const char *s, int pos2) const;            //compare函數在>時返回1,<時返回-1,==時返回0  

string的子串:

  string substr(int pos = 0,int n = npos) const;                          //返回pos開始的n個字元組成的字元串

string的交換:

  void swap(string &s2);                                     //交換當前字元串與s2的值

string類的查找函數:

  int find(char c, int pos = 0) const;                             //從pos開始查找字元c在當前字元串的位置 
  int find(const char *s, int pos = 0) const;                  //從pos開始查找字元串s在當前串中的位置 
  int find(const char *s, int pos, int n) const;               //從pos開始查找字元串s中前n個字元在當前串中的位置 
  int find(const string &s, int pos = 0) const;               //從pos開始查找字元串s在當前串中的位置
  (註:,find函數查找成功時返回所在位置,失敗返回string::npos的值。string 類將 npos 定義為保證大於任何有效下標的值.一般為-1)
  int rfind(char c, int pos = npos) const;                     //從pos開始從後向前查找字元c在當前串中的位置 
  int rfind(const string &s,int pos = npos) const;
  int rfind(const  char *s, int pos = npos) const;         //從pos開始從後向前查找字元串s在當前串中的位置
  int rfind(const char *s, int pos, int n = npos) const; //從pos開始從後向前查找字元串s中前n個字元組成的字元串在當前串中的位置
     // 以上rfind函數中查找成功返回所在位置,失敗時返回string::npos的值 
  int find_first_of(char c, int pos = 0) const;                 //從pos開始查找字元c第一次出現的位置 
  int find_first_of(const string &s,int pos = 0)  const;
  int find_first_of(const char *s, int pos = 0) const;      //從pos開始查找字元串s第一次出現的位置
  int find_first_of(const char *s, int pos, int n) const;    //從pos開始查找當前串中第一個在s的前n個字元組成的數組裡的字元的位置。(查找失敗返回string::npos )
  int find_first_not_of(char c, int pos = 0) const; 
  int find_first_not_of(const char *s, int pos = 0) const; 
  int find_first_not_of(const char *s, int pos,int n) const; 
  int find_first_not_of(const string &s,int pos = 0) const; //從當前串中查找第一個不在串s中的字元出現的位置,失敗返回string::npos 
  int find_last_of(char c, int pos = npos) const; 
  int find_last_of(const char *s, int pos = npos) const; 
  int find_last_of(const char *s, int pos, int n = npos) const; 
  int find_last_of(const string &s,int pos = npos) const; 
  int find_last_not_of(char c, int pos = npos) const; 
  int find_last_not_of(const char *s, int pos = npos) const; 
  int find_last_not_of(const char *s, int pos, int n) const; 
  int find_last_not_of(const string &s,int pos = npos) const;   //find_last_of和find_last_not_of與find_first_of和find_first_not_of相似,只不過是從後向前查找

string類的替換函數:

  string &replace(int p0, int n0,const char *s);                                   //刪除從p0開始的n0個字元,然後在p0處插入串s 
  string &replace(int p0, int n0,const char *s, int n);                          //刪除p0開始的n0個字元,然後在p0處插入字元串s的前n個字元 
  string &replace(int p0, int n0,const string &s);                                //刪除從p0開始的n0個字元,然後在p0處插入串s 
  string &replace(int p0, int n0,const string &s, int pos, int n);            //刪除p0開始的n0個字元,然後在p0處插入串s中從pos開始的n個字元 
  string &replace(int p0, int n0,int n, char c);                                    //刪除p0開始的n0個字元,然後在p0處插入n個字元c 
  string &replace(iterator first0, iterator last0,const char *s);            //把[first0,last0)之間的部分替換為字元串s 
  string &replace(iterator first0, iterator last0,const char *s, int n);     //把[first0,last0)之間的部分替換為s的前n個字元 
  string &replace(iterator first0, iterator last0,const string &s);         //把[first0,last0)之間的部分替換為串s 
  string &replace(iterator first0, iterator last0,int n, char c);            //把[first0,last0)之間的部分替換為n個字元c 
  string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);           //把[first0,last0)之間的部分替換成[first,last)之間的字元串

string類的插入函數:

  string &insert(int p0, const char *s); 
  string &insert(int p0, const char *s, int n); 
  string &insert(int p0,const string &s); 
  string &insert(int p0,const string &s, int pos, int n);                     //前4個函數在p0位置插入字元串s或串s中pos開始的前n個字元 
  string &insert(int p0, int n, char c);                                           //此函數在p0處插入n個字元c 
  iterator insert(iterator it, char c);                                              //在it處插入字元c,返回插入後迭代器的位置 
  void insert(iterator it, const_iterator first, const_iterator last);    //在it處插入[first,last)之間的字元 
  void insert(iterator it, int n, char c);                                         //在it處插入n個字元c

string類的刪除函數:

  iterator erase(iterator first, iterator last);                                  //刪除[first,last)之間的所有字元,返回刪除後迭代器的位置 
  iterator erase(iterator it);                                                        //刪除it指向的字元,返回刪除後迭代器的位置 
  string &erase(int pos = 0, int n = npos);                                  //刪除pos開始的n個字元,返回修改後的字元串

string類的迭代器處理: 
  string類提供了向前和向後遍歷的迭代器iterator,迭代器提供了訪問各個字元的語法,類似於指針操作,迭代器不檢查範圍。 
  用string::iterator或string::const_iterator聲明迭代器變數,const_iterator不允許改變迭代的內容。常用迭代器函數有: 
  const_iterator begin()const; iterator begin();                //返回string的起始位置 
  const_iterator end()const; iterator end();                    //返回string的最後一個字元後面的位置 
  const_iterator rbegin()const; iterator rbegin();            //返回string的最後一個字元的位置 
  const_iterator rend()const; iterator rend();                 //返回string第一個字元位置的前面
  // rbegin和rend用於從後向前的迭代訪問,通過設置迭代器string::reverse_iterator,string::const_reverse_iterator實現

用stringstream類處理字元串流:
  通過定義ostringstream、istringstream或stringstream實現對,包含在<sstream>頭文件中 例如:     
  string input("hello,this is a test");     
  istringstream is(input);     
  string s1,s2,s3,s4;     
  is>>s1>>s2>>s3>>s4;        //s1="hello,this",s2="is",s3="a",s4="test"     
  ostringstream os;     
  os<<s1<<s2<<s3<<s4;     
  cout<<os.str();


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

-Advertisement-
Play Games
更多相關文章
  • Please enter a commit message to explain why this merge is necessary. 請輸入提交消息來解釋為什麼這種合併是必要的 git 在pull或者合併分支的時候有時會遇到這個界面。可以不管(直接下麵3,4步),如果要輸入解釋的話就需要: 1
  • 恢復內容開始 一、ACTIVEX及其相關概念 使用 WindowsFormsHost 元素可將 Windows Forms控制項放置到 WPF 元素或頁面中。 若要在 Windows Forms控制項或窗體中承載 WPF 元素,使用 ElementHost控制項         System.Wind
  • String 字元串常量StringBuffer 字元串變數(線程安全)StringBuilder 字元串變數(非線程安全) 簡要的說, String 類型和 StringBuffer 類型的主要性能區別其實在於 String 是不可變的對象, 因此在每次對 String 類型進行改變的時候其實都等
  • 設置frame的scrolling="yes",在右側頁面的body裡加入: style="overflow-x:hidden;"  如:<body style="overflow-x:hidden;">
  •   添加→ 方法參數中有一個有關添加視圖模型類型的形參,比如vm→ 根據vm的某個屬性,比如Name判斷在上下文中是否存在,如果不存在就拋EntityNotFoundException異常→ 判斷vm所依賴的外鍵那對應的那個實體是否存在,比如vm中有各PoductCategoryId外鍵,就判斷下上
  • 字元串在Python內部的表示是unicode編碼,因此,在做編碼轉換時,通常需要以unicode作為中間編碼,即先將其他編碼的字元串解碼(decode)成unicode,再從unicode編碼(encode)成另一種編碼。 decode的作用是將其他編碼的字元串轉換成unicode編碼,如str1
一周排行
    -Advertisement-
    Play Games
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...
  • 目錄前言PostgreSql安裝測試額外Nuget安裝Person.cs模擬運行Navicate連postgresql解決方案Garnet為什麼要選擇Garnet而不是RedisRedis不再開源Windows版的Redis是由微軟維護的Windows Redis版本老舊,後續可能不再更新Garne ...
  • C#TMS系統代碼-聯表報表學習 領導被裁了之後很快就有人上任了,幾乎是無縫銜接,很難讓我不想到這早就決定好了。我的職責沒有任何變化。感受下來這個系統封裝程度很高,我只要會調用方法就行。這個系統交付之後不會有太多問題,更多應該是做小需求,有大的開發任務應該也是第二期的事,嗯?怎麼感覺我變成運維了?而 ...
  • 我在隨筆《EAV模型(實體-屬性-值)的設計和低代碼的處理方案(1)》中介紹了一些基本的EAV模型設計知識和基於Winform場景下低代碼(或者說無代碼)的一些實現思路,在本篇隨筆中,我們來分析一下這種針對通用業務,且只需定義就能構建業務模塊存儲和界面的解決方案,其中的數據查詢處理的操作。 ...
  • 對某個遠程伺服器啟用和設置NTP服務(Windows系統) 打開註冊表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer 將 Enabled 的值設置為 1,這將啟用NTP伺服器功 ...
  • title: Django信號與擴展:深入理解與實踐 date: 2024/5/15 22:40:52 updated: 2024/5/15 22:40:52 categories: 後端開發 tags: Django 信號 松耦合 觀察者 擴展 安全 性能 第一部分:Django信號基礎 Djan ...
  • 使用xadmin2遇到的問題&解決 環境配置: 使用的模塊版本: 關聯的包 Django 3.2.15 mysqlclient 2.2.4 xadmin 2.0.1 django-crispy-forms >= 1.6.0 django-import-export >= 0.5.1 django-r ...
  • 今天我打算整點兒不一樣的內容,通過之前學習的TransformerMap和LazyMap鏈,想搞點不一樣的,所以我關註了另外一條鏈DefaultedMap鏈,主要調用鏈為: 調用鏈詳細描述: ObjectInputStream.readObject() DefaultedMap.readObject ...
  • 後端應用級開發者該如何擁抱 AI GC?就是在這樣的一個大的浪潮下,我們的傳統的應用級開發者。我們該如何選擇職業或者是如何去快速轉型,跟上這樣的一個行業的一個浪潮? 0 AI金字塔模型 越往上它的整個難度就是職業機會也好,或者說是整個的這個運作也好,它的難度會越大,然後越往下機會就會越多,所以這是一 ...
  • @Autowired是Spring框架提供的註解,@Resource是Java EE 5規範提供的註解。 @Autowired預設按照類型自動裝配,而@Resource預設按照名稱自動裝配。 @Autowired支持@Qualifier註解來指定裝配哪一個具有相同類型的bean,而@Resourc... ...