string是C++標準模板庫中專門用于字串處理的資料結構型別,它并不是 C++的基本資料型別,它是 C++標準模板庫中的一個“類”,若要使用 string 物件,則必須包含頭檔案#include <string>,
- 初始化
常用的初始化有以下幾種,帶等號的是拷貝初始化,
string str1("hello world"); // hello world
string str2 = "hello world"; // hello world
string str3(str2); // hello world
string str4 = str3; // hello world
string str5(5,'d'); // ddddd
string str6(str2, 6); // world,從str2的第6個字符開始到結束,拷貝到str6中
string str7(str2, 0, 5); // hello, 從str2的第0個字符開始拷貝5個字符到str7中
char buff[] = "hello sorld";
string str8(buff, 5); // hello, 拷貝buff的前5個字符到str8中
特殊資料結構成員
static const size_t npos = -1;
- string 的基本操作
- 長度
size_t length() const noexcept; // 得到字串的長度
size_t size() const noexcept; // 得到字串的長度
size_t max_size() const noexcept; // 得到字串可以達到的最大長度
- 插入
在指定的位置后面插入一個字串
// 在pos后面插入字串str
string& insert (size_t pos, const string& str);
// 在pos后面插入str的subpos處往后的sublen長度的字串
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);
// 在pos后面插入字符陣列s
string& insert (size_t pos, const char* s);
// 在pos后面插入字符陣列s的前n個字符
string& insert (size_t pos, const char* s, size_t n);
// 在pos后面插入n個字符c
string& insert (size_t pos, size_t n, char c);
// 在p后面插入n個字符c
iterator insert (const_iterator p, size_t n, char c);
// 在p后面插入一個字符c
iterator insert (const_iterator p, char c);
// 在p后面插入迭代器first到last之間的字串
template <class InputIterator>
iterator insert (iterator p, InputIterator first, InputIterator last);
// 在p后面插入il內的所有字符
string& insert (const_iterator p, initializer_list<char> il);
- 替換
把指定的位置后指定長度的字串替換成另一個字串
// 把pos后面len長度的字串替換成str
string& replace (size_t pos, size_t len, const string& str);
// 把i1和i2之間的內容替換成str
string& replace (const_iterator i1, const_iterator i2, const string& str);
// 把pos后面len長度的字串替換成字串str的subpos后面的sublen個長度的字串
string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
// 把pos后面len長度的字串替換成字符陣列s里的所有內容
string& replace (size_t pos, size_t len, const char* s);
// 把i1和i2之間的字串替換成陣列s里的所有內容
string& replace (const_iterator i1, const_iterator i2, const char* s);
// 把pos后面len長度的字串替換成字符陣列s里面前n個字符
string& replace (size_t pos, size_t len, const char* s, size_t n);
// 把i1和i2之間的字串替換成字符陣列s里面的前n個字符
string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
// 把pos后面len長度的字串替換成n個字符c
string& replace (size_t pos, size_t len, size_t n, char c);
// 把i1和i2之間的字串替換成n個字符c
string& replace (const_iterator i1, const_iterator i2, size_t n, char c);
// 把i1和i2之間的字串替換成迭代器first與last之間的內容
template <class InputIterator>
string& replace (const_iterator i1, const_iterator i2, InputIterator first, InputIterator last);
// 把字串i1和i2之間的內容替換成il里的所有字符
string& replace (const_iterator i1, const_iterator i2, initializer_list<char> il);
- 添加
在字串的末尾添加另一個字串的內容
// 在字串的末尾添加另一個字串str
string& append (const string& str);
string& operator+= (const string& str);
// 在字串的末尾添加兩一個字串str的subpos后面sublen長度的字串
string& append (const string& str, size_t subpos, size_t sublen);
// 在字串的末尾添加字符陣列s里的所有內容
string& append (const char* s);
string& operator+= (const char* s);
// 在字串的末尾添加字符陣列s的前n個字符
string& append (const char* s, size_t n);
// 在字串的末尾添加n個字符c
string& append (size_t n, char c);
// 在字串的末尾添加一個字符c
string& operator+= (char c);
// 在字串的末尾添加迭代器first與last之間的字符
template <class InputIterator>
string& append (InputIterator first, InputIterator last);
// 在字串的末尾添加il里的所有內容
string& append (initializer_list<char> il);
string& operator+= (initializer_list<char> il);
- 賦值
用新的字串替換掉本字串的內容
// 用str替換掉本字串的內容
string& assign (const string& str);
string& operator= (const string& str);
// 用str里的subpos后面sublen個長度的字串替換掉本字串的內容
string& assign (const string& str, size_t subpos, size_t sublen);
// 用字符陣列s里的的所有字符替換掉本字串的內容
string& assign (const char* s);
string& operator= (const char* s);
// 用字符陣列s的前n個字符替換掉本字串里的內容
string& assign (const char* s, size_t n);
// 把本字串替換成n個字符c
string& assign (size_t n, char c);
// 用迭代器first和last之間的字符替換掉本字串的內容
template <class InputIterator>
string& assign (InputIterator first, InputIterator last);
// 用il里的所有字串替換掉本字串的內容
string& assign (initializer_list<char> il);
string& operator= (initializer_list<char> il);
// 用str替換掉本字串的內容
string& assign (string&& str) noexcept;
string& operator= (string&& str) noexcept;
// 將字串的長度置為1,并把字符C填充到字串里
string& operator= (char c);
- 洗掉
洗掉指定位置后面指定長度的字符
// 洗掉pos后面len個長度的字符
string& erase (size_t pos = 0, size_t len = npos);
// 洗掉迭代器p到末尾的所有字符
iterator erase (iterator p);
// 洗掉迭代器first與last之間的字符
iterator erase (iterator first, iterator last);
- 清空
清空字串,得到一個空的字串
// 清空字串
void clear() noexcept;
- 為空
判斷字串的內容是否為空
// 判斷字串是否為空
bool empty() const noexcept;
- 剪切
得到指定位置后面指定長度的字串
// 回傳pos后面len個長度的字串
string substr (size_t pos = 0, size_t len = npos) const;
- 比較
指定位置后面指定長度的字串與另一個字串進行比較
回傳值:0,兩個字串相等;
<0,參與比較的字串不匹配的第一個字符的值較低,或者所有比較的字符都匹配但參與比較的字串較短;
>0,參與比較的字串不匹配的第一個字符的值更大,或者所有比較的字符都匹配但參與比較的字串更長,
// 本字串與str進行比較
int compare (const string& str) const noexcept;
// 本字串pos后面len長度的字串與str進行比較
int compare (size_t pos, size_t len, const string& str) const;
// 本字串pos后面len長度的字串與str的subpos位置后面sublen長度的字串進行比較
int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;
// 本字串與字符陣列的所有字符進行比較
int compare (const char* s) const;
// 本字串pos后面len長度的字串與字符陣列s里的所有字符進行比較
int compare (size_t pos, size_t len, const char* s) const;
// 本字串pos后面len長度的字串與字符陣列s里前n個字符進行比較
int compare (size_t pos, size_t len, const char* s, size_t n) const;
- 交換
與另一個字串交換它們的內容
// 與str交換內容
void swap (string& str);
- 修改長度
請求修改字串容量的大小,長度最多為n個字符,
如果n大于當前的字串容量,則該函式會使容器將其容量增加到n個字符(或更大),
在所有其他情況下,它都被視為縮小字串容量的非系結請求:容器實作可以自由地進行優化,并使字串的容量大于n,
此函式對字串長度沒有影響,也不能改變其內容,
// 將字串的長度重置為n
void reserve (size_t n = 0);
- 重設
把字串重新設定成指定的長度的字符
如果重設后的長度小于原長度,洗掉多余的字符
如果重設后的長度大于原長度,則在結尾處插入空字符或指定的字符到達指定的長度
// 將字串重設成n個長度的空字串
void resize (size_t n);
// 將字串重設成n個長度的c字符
void resize (size_t n, char c);
- 洗掉末尾字符
洗掉字串末尾的一個字符
// 彈出字串末尾的一個字符
void pop_back();
- 在末尾添加字符
在字串的末尾添加一個字符
// 在字串的末尾添加一個字符c
void push_back (char c);
- 得到C型別的字串
轉換成等價的C字串
// 轉換成等價的C字串
const char* c_str() const noexcept;
// 得到該字串的陣列指標
const char* data() const noexcept;
- 取字符
得到指定位置處的字符
// 得到指定位置處的字符
char& at (size_t pos);
const char& at (size_t pos) const;
char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
// 得到字串的第一個字符
char& front();
const char& front() const;
// 得到字串的最后一個字符
char& back();
const char& back() const;
- 拷貝
將字串的指定內容拷貝到字符陣列里
// 將字串pos處開始的len長度的字串拷貝到字符陣列s里面
size_t copy (char* s, size_t len, size_t pos = 0) const;
- 交換
交換兩個字串里的內容
// 交換x換y的值
void swap (string& x, string& y);
- 查找
查找字串中指定字符或字串出現的第一處位置
如果沒有匹配的,回傳string::npos
// 從字串的pos處開始查找與字串str相同的字串
size_t find (const string& str, size_t pos = 0) const;
// 從字串的pos處開始查找與字符陣列s相同的字串
size_t find (const char* s, size_t pos = 0) const;
// 從字串的pos處開始長度為n的范圍內查找與字符陣列s相同的字串
size_t find (const char* s, size_t pos, size_t n) const;
// 從字串串的pos處開始查找與字符c相同的字符
size_t find (char c, size_t pos = 0) const;
查找字串中指定字符或字串出現的最后一處位置
如果沒有匹配的,回傳string::npos
// 查找在字串pos之前的最后一個與字串str相匹配的字串的位置
size_t rfind (const string& str, size_t pos = npos) const noexcept;
// 查找在字串pos之前的最后一個與字符陣列s相匹配的字串的位置
size_t rfind (const char* s, size_t pos = npos) const;
// 查找在字串pos之前的n個字符內最后一個與字符陣列s相匹配的字串的位置
size_t rfind (const char* s, size_t pos, size_t n) const;
// 查找在字串pos之前的最后一個與字符c匹配的字符的位置
size_t rfind (char c, size_t pos = npos) const noexcept;
查找字串中與其引數中指定的任何字符匹配的第一個字符
如果沒有匹配的,回傳string::npos
// 查找字串中的pos處開始與字串str里的任一字符相同的第一個位置
size_t find_first_of (const string& str, size_t pos = 0) const noexcept;
// 查找字串中的pos處開始與字符陣列s里的任一字符相同的第一個位置
size_t find_first_of (const char* s, size_t pos = 0) const;
// 查找字串中的pos處開始長度為n的字串內與字符陣列s里的任一字符相同的第一個位置
size_t find_first_of (const char* s, size_t pos, size_t n) const;
// 查找字串中的pos處開始第一個與字符c相同的位置
size_t find_first_of (char c, size_t pos = 0) const noexcept;
查找字串中與其引數中指定的任何字符匹配的最后一個字符
如果沒有匹配的,回傳string::npos
// 查找字串中的pos之前的所有字符與字串str里的任一字符相同的最后一個位置
size_t find_last_of (const string& str, size_t pos = npos) const noexcept;
// 查找字串中的pos之前的所有字符與字符陣列s里的任一字符相同的最后一個位置
size_t find_last_of (const char* s, size_t pos = npos) const;
// 查找字串中的pos之前長度為n的字串內與字符陣列s里的任一字符相同的最后一個位置
size_t find_last_of (const char* s, size_t pos, size_t n) const;
// 查找字串中的pos之前的所有字符里最后一個與字符c相同的位置
size_t find_last_of (char c, size_t pos = npos) const noexcept;
查找字串中與指定的字符陣列或字串里的任一字符都不匹配的第一個位置
如果沒有找到(即參與比較的部分完全相同),回傳string::npos
// 比較字串pos后面的全部字符與字串str里的任一字符都不同的第一個字符所在的位置
size_t find_first_not_of (const string& str, size_t pos = 0) const noexcept;
// 比較字串pos后面的全部字符與字符陣列s里的任一字符都不同的第一個字符所在的位置
size_t find_first_not_of (const char* s, size_t pos = 0) const;
// 比較字串pos后面的n個字符與字符陣列s里的任一字符都不同的第一個字符所在的位置
size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
// 比較字串pos后面的全部字符與字符c不同的第一個字符所在的位置
size_t find_first_not_of (char c, size_t pos = 0) const noexcept;
查找字串中與指定的字符陣列或字串里的任一字符都不匹配的最后一個位置
如果沒有找到(即參與比較的部分完全相同),回傳string::npos
// 比較字串pos前面的全部字符與字串str里的任一字符都不同的最后一個字符所在的位置
size_t find_last_not_of (const string& str, size_t pos = npos) const noexcept;
// 比較字串pos前面的全部字符與字符陣列s里的任一字符都不同的最后一個字符所在的位置
size_t find_last_not_of (const char* s, size_t pos = npos) const;
// 比較字串pos前面的n個字符與字符陣列s里的任一字符都不同的最后一個字符所在的位置
size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
// 比較字串pos前面的全部字符與字符c不同的最后一個字符所在的位置
size_t find_last_not_of (char c, size_t pos = npos) const noexcept;
- operator
多載+運算子
回傳一個新構造的字串物件,其值為lhs中的字符和rhs中的字符的連接
string operator+ (const string& lhs, const string& rhs);
string operator+ (string&& lhs, string&& rhs);
string operator+ (string&& lhs, const string& rhs);
string operator+ (const string& lhs, string&& rhs);
string operator+ (const string& lhs, const char* rhs);
string operator+ (string&& lhs, const char* rhs);
string operator+ (const char* lhs, const string& rhs);
string operator+ (const char* lhs, string&& rhs);
string operator+ (const string& lhs, char rhs);
string operator+ (string&& lhs, char rhs);
string operator+ (char lhs, const string& rhs);
string operator+ (char lhs, string&& rhs);
多載==運算子
比較lhs與rhs是否相等
bool operator== (const string& lhs, const string& rhs) noexcept;
bool operator== (const char* lhs, const string& rhs);
bool operator== (const string& lhs, const char* rhs);
多載!=運算子
比較lhs與rhs是否不等
bool operator!= (const string& lhs, const string& rhs) noexcept;
bool operator!= (const char* lhs, const string& rhs);
bool operator!= (const string& lhs, const char* rhs);
多載<運算子
比較lhs是否小于rhs
bool operator< (const string& lhs, const string& rhs) noexcept;
bool operator< (const char* lhs, const string& rhs);
bool operator< (const string& lhs, const char* rhs);
多載<=運算子
比較lhs是否小于等于rhs
bool operator<= (const string& lhs, const string& rhs) noexcept;
bool operator<= (const char* lhs, const string& rhs);
bool operator<= (const string& lhs, const char* rhs);
多載>運算子
比較lhs是否大于rhs
bool operator> (const string& lhs, const string& rhs) noexcept;
bool operator> (const char* lhs, const string& rhs);
bool operator> (const string& lhs, const char* rhs);
多載>=運算子
比較lhs是否大于等于rhs
bool operator>= (const string& lhs, const string& rhs) noexcept;
bool operator>= (const char* lhs, const string& rhs);
bool operator>= (const string& lhs, const char* rhs);
- 輸入輸出流
從流中插入/獲取字串
// 從流中提取字串
istream& operator>> (istream& is, string& str);
// 將字串插入流
ostream& operator<< (ostream& os, const string& str);
// 從流is中提取一行字串到str中,直到劃分字符delim為止
istream& getline (istream& is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
// 從流is中提取一行字串到str中,直到換行為止
istream& getline (istream& is, string& str);
istream& getline (istream&& is, string& str);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/469593.html
標籤:其他
上一篇:C++基礎-7-多型
下一篇:C++基礎-檔案操作
