string類的構造函數:
string(const char *s); //用c字元串s初始化string(int n,char c); //用n個字元c初始化
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中對象字元的處理,有很多已有的函數在CCtype頭檔案中,可以很友善的應用
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在目前串中的位置
//查找成功時傳回所在位置,失敗傳回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變量實作,<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();
标準C++庫字元串類std::string的用法
#include<string>
std::string s1;
std::string s3(s2);
std::string s2("this is a string");
begin 得到指向字元串開頭的Iterator
end 得到指向字元串結尾的Iterator
rbegin 得到指向反向字元串開頭的Iterator
rend 得到指向反向字元串結尾的Iterator
size 得到字元串的大小
length() 和size函數功能相同
max_size 字元串可能的最大大小
capacity 在不重新配置設定記憶體的情況下,字元串可能的大小
empty 判斷是否為空
operator[] 取第幾個元素,相當于數組
c_str 取得C風格的const char* 字元串
data 取得字元串内容位址
operator= 指派操作符
reserve 預留白間
swap 交換函數
insert 插入字元
append 追加字元
push_back 追加字元
erase 删除字元串
clear 清空字元容器中所有内容
resize 重新配置設定空間
assign 和指派操作符一樣
replace 替代
copy 字元串到空間
find 查找,傳回基于0的索引号
rfind 反向查找
find_first_of 查找包含子串中的任何字元,傳回第一個位置
find_first_not_of 查找不包含子串中的任何字元,傳回第一個位置
find_last_of 查找包含子串中的任何字元,傳回最後一個位置
find_last_not_of 查找不包含子串中的任何字元,傳回最後一個位置
substr(n1,len) 得到字元串從n1開始的長度為len的子串
比較字元串(支援所有的關系運算符)
compare 比較字元串
operator+ 字元串連結
operator+= += 操作符
operator== 判斷是否相等
operator!= 判斷是否不等于
operator< 判斷是否小于
operator>> 從輸入流中讀入字元串
operator<< 字元串寫入輸出流
getline 從輸入流中讀入一行