首頁 web前端 js教程 C 中的string類別的用法小結_javascript技巧

C 中的string類別的用法小結_javascript技巧

May 16, 2016 pm 03:46 PM
c++ string類

相信使用過MFC程式設計的朋友對CString這個類別的印象應該要非常深刻吧?的確,MFC中的CString類別使用起來真的非常的方便好用。但是如果離開了MFC框架,還有沒有這樣使用起來非常方便的類別呢?答案是肯定的。也許有人會說,即使不用MFC框架,也可以想辦法使用MFC中的API,具體的操作方法在本文最後給出操作方法。其實,可能很多人很可能會忽略掉標準C 中string類別的使用。標準C 中提供的string類得功能也是非常強大的,一般都能滿足我們開發專案時使用。現將具體用法的一部分羅列如下,只起一個拋磚引玉的作用吧,好了,廢話少說,直接進入正題吧!

要想使用標準C 中string類,必須要包含

#include // 注意是,不是,帶.h的是C語言中的頭檔

using  std::string;

using  std::wstring;

using namespace std;

下面你就可以使用string/wstring了,它們兩分別對應著char和wchar_t。

string和wstring的用法是一樣的,以下只用string作介紹:

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中

                                          ——大小
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在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值 
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值 
int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) 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中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实现

字符串流处理:

通过定义ostringstream和istringstream变量实现,#include 头文件中
例如:

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();

登入後複製

以上就是对C++ string类的一个简要介绍。用的好的话它所具有的功能不会比MFC中的CString类逊色多少,呵呵,个人意见!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

char在C語言字符串中的作用是什麼 char在C語言字符串中的作用是什麼 Apr 03, 2025 pm 03:15 PM

在 C 語言中,char 類型在字符串中用於:1. 存儲單個字符;2. 使用數組表示字符串並以 null 終止符結束;3. 通過字符串操作函數進行操作;4. 從鍵盤讀取或輸出字符串。

在Docker環境中使用PECL安裝擴展時為什麼會報錯?如何解決? 在Docker環境中使用PECL安裝擴展時為什麼會報錯?如何解決? Apr 01, 2025 pm 03:06 PM

在Docker環境中使用PECL安裝擴展時報錯的原因及解決方法在使用Docker環境時,我們常常會遇到一些令人頭疼的問�...

c上標3下標5怎麼算 c上標3下標5算法教程 c上標3下標5怎麼算 c上標3下標5算法教程 Apr 03, 2025 pm 10:33 PM

C35 的計算本質上是組合數學,代表從 5 個元素中選擇 3 個的組合數,其計算公式為 C53 = 5! / (3! * 2!),可通過循環避免直接計算階乘以提高效率和避免溢出。另外,理解組合的本質和掌握高效的計算方法對於解決概率統計、密碼學、算法設計等領域的許多問題至關重要。

c語言多線程的四種實現方式 c語言多線程的四種實現方式 Apr 03, 2025 pm 03:00 PM

語言多線程可以大大提升程序效率,C 語言中多線程的實現方式主要有四種:創建獨立進程:創建多個獨立運行的進程,每個進程擁有自己的內存空間。偽多線程:在一個進程中創建多個執行流,這些執行流共享同一內存空間,並交替執行。多線程庫:使用pthreads等多線程庫創建和管理線程,提供了豐富的線程操作函數。協程:一種輕量級的多線程實現,將任務劃分成小的子任務,輪流執行。

distinct函數用法 distance函數c  用法教程 distinct函數用法 distance函數c 用法教程 Apr 03, 2025 pm 10:27 PM

std::unique 去除容器中的相鄰重複元素,並將它們移到末尾,返回指向第一個重複元素的迭代器。 std::distance 計算兩個迭代器之間的距離,即它們指向的元素個數。這兩個函數對於優化代碼和提升效率很有用,但也需要注意一些陷阱,例如:std::unique 只處理相鄰的重複元素。 std::distance 在處理非隨機訪問迭代器時效率較低。通過掌握這些特性和最佳實踐,你可以充分發揮這兩個函數的威力。

蛇形命名法在C語言中如何應用? 蛇形命名法在C語言中如何應用? Apr 03, 2025 pm 01:03 PM

C語言中蛇形命名法是一種編碼風格約定,使用下劃線連接多個單詞構成變量名或函數名,以增強可讀性。儘管它不會影響編譯和運行,但冗長的命名、IDE支持問題和歷史包袱需要考慮。

C  中releasesemaphore的用法 C 中releasesemaphore的用法 Apr 04, 2025 am 07:54 AM

C 中 release_semaphore 函數用於釋放已獲得的信號量,以便其他線程或進程訪問共享資源。它將信號量計數增加 1,允許阻塞的線程繼續執行。

C   程序員&#s未定義行為指南 C 程序員&#s未定義行為指南 Apr 03, 2025 pm 07:57 PM

探索C語言編程的未定義行為:一本詳盡指南本文介紹一本關於C語言編程中未定義行為的電子書,共12章,涵蓋了C語言編程中一些最棘手和鮮為人知的方面。本書並非C語言入門教材,而是面向熟悉C語言編程的讀者,深入探討未定義行為的各種情況及其潛在後果。作者DmitrySviridkin,編輯AndreyKarpov。歷經六個月的精心準備,這本電子書終於與讀者見面。未來還將推出印刷版。本書最初計劃包含11章,但在創作過程中,內容不斷豐富,最終擴展到12章——這本身就是一個經典的數組越界案例,可謂是每個C程序員

See all articles