一、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 | 讀取一行,存盤在字串中 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/501167.html
標籤:其他
上一篇:12-Java中執行緒的狀態型別
