jizhicmsseo排名是什么意思
string容器
- 概念
- 本質(zhì)
- string和char 區(qū)別:
- 特點
- string構(gòu)造函數(shù)
- 構(gòu)造函數(shù)原型
- string賦值操作
- 賦值的函數(shù)原型
- 示例
- string字符串拼接
- 函數(shù)原型:
- 示例
- string查找和替換
- 函數(shù)原型
- 示例
- string字符串比較
- 比較方式 字符串比較是按字符的ASCII碼進行對比
- 函數(shù)原型
- 示例
- string字符存取
- string中**單個字符存取方式**有兩種
- 使用下標(biāo)(`[]`)運算符
- 使用`at()`函數(shù)
- string插入和刪除
- 函數(shù)原型
- 示例
- string子串
- 函數(shù)原型
- 示例
概念
在C++中是C++標(biāo)準(zhǔn)庫提供的一個字符串類模板。它被設(shè)計為更高級、更方便和更安全的字符串處理工具,相比于C風(fēng)格的字符串?dāng)?shù)組,它提供了更多的功能和便捷的操作。
string類封裝了一系列成員函數(shù)來操作字符串,這些成員函數(shù)包括字符串的創(chuàng)建、復(fù)制、拼接、查找、替換等等。它還提供了運算符重載,使得字符串的操作和使用更加直觀和簡潔。
當(dāng)使用string時,不需要手動管理字符串的內(nèi)存,它會自動處理內(nèi)存的分配和釋放。此外,string還提供了對字符串的邊界檢查,避免了緩沖區(qū)溢出等問題,提高了代碼的安全性。
本質(zhì)
string本質(zhì)上是一個類
由于string是一個類,所以它可以使用類的特性,如構(gòu)造函數(shù)、析構(gòu)函數(shù)、拷貝構(gòu)造函數(shù)、賦值運算符重載等。這些特性使得string類更易于使用和管理字符串?dāng)?shù)據(jù)。
綜上所述,string是C++風(fēng)格的字符串,本質(zhì)上是一個類,在C++中廣泛用于處理和操作字符串?dāng)?shù)據(jù),提供了便利、安全和高級的字符串處理功能。
string和char 區(qū)別:
- char * 是一個指針
- string是一個類,類內(nèi)部封裝了char*,管理這個字符串,是一個char*型的容器。
- 內(nèi)存管理:string類封裝了字符串的內(nèi)存管理,它會自動分配和釋放內(nèi)存,并且可以動態(tài)調(diào)整字符串的大小。而char*需要手動分配和釋放內(nèi)存,并且在操作字符串時需要確保足夠的內(nèi)存空間。
- 字符串長度::string對象可以存儲任意長度的字符串,并且可以使用成員函數(shù)獲取字符串的長度。而char*作為一個字符數(shù)組指針,需要以空字符(‘\0’)作為字符串的結(jié)尾,使用標(biāo)準(zhǔn)庫函數(shù)如strlen()來獲取其長度。
- 操作和修改:string提供了一系列成員函數(shù)來進行字符串的操作和修改,如拼接、插入、刪除、查找、替換等。而char*需要使用標(biāo)準(zhǔn)庫函數(shù)來進行類似的操作,如strcat()、strcpy()、strncpy()等。
- 安全性:由于string類內(nèi)部封裝了字符串的操作和內(nèi)存管理,對于越界訪問和緩沖區(qū)溢出等問題有更好的安全性。而char*需要手動確保字符串操作的安全,容易發(fā)生內(nèi)存訪問錯誤。
特點
-
封裝了一些成員方法:std::string類提供了許多有用的成員方法來操作字符串,包括查找(find())、拷貝(copy())、刪除(erase())、替換(replace())、插入(insert())等。這些成員方法使得字符串的操作更為便捷和靈活。
-
內(nèi)存管理:std::string對象會自動管理分配給它的內(nèi)存空間,包括創(chuàng)建、擴展和釋放。這意味著你無需手動管理字符串的內(nèi)存,避免了復(fù)制越界和取值越界等問題。類內(nèi)部會負責(zé)處理內(nèi)存的分配和釋放,大大提高了程序的安全性和可靠性。
string構(gòu)造函數(shù)
構(gòu)造函數(shù)原型
string(const char* s);
使用以空字符(‘\0’)結(jié)尾的C風(fēng)格字符串s來初始化字符串對象。
例如:std::string str(“Hello”);
string(const string& str);
使用另一個std::string對象str來初始化字符串對象。
例如:std::string str1(“Hello”); std::string str2(str1);
string(int n, char c);
使用字符c重復(fù)n次初始化字符串對象。
例如:std::string str(5, ‘a(chǎn)’); // str的值為 “aaaaa”
string();
創(chuàng)建一個空的字符串對象。
例如:std::string str;
string賦值操作
賦值的函數(shù)原型
string& operator=(const char* s);
//char*類型字符串 賦值給當(dāng)前的字符串string& operator=(const string &s);
//把字符串s賦給當(dāng)前的字符串string& operator=(char c);
//字符賦值給當(dāng)前的字符串string& assign(const char *s);
//把字符串s賦給當(dāng)前的字符串string& assign(const char *s, int n);
//把字符串s的前n個字符賦給當(dāng)前的字符串string& assign(const string &s);
//把字符串s賦給當(dāng)前字符串string& assign(int n, char c);
//用n個字符c賦給當(dāng)前字符串
示例
std::string str1;
str1 = "Hello"; // 使用const char*類型字符串進行賦值
// 等價于 str1.operator=("Hello");std::string str2;
std::string anotherStr = "World";
str2 = anotherStr; // 使用另一個std::string對象進行賦值
// 等價于 str2.operator=(anotherStr);std::string str3;
str3 = 'A'; // 使用字符進行賦值
// 等價于 str3.operator=('A');std::string str4;
str4.assign("Hello"); // 使用const char*類型字符串進行賦值
// 等價于 str4.assign("Hello", std::strlen("Hello"));std::string str5;
str5.assign("Hello", 3); // 使用字符串的前3個字符進行賦值
// 等價于 str5.assign("Hel", 3);std::string str6;
std::string anotherStr2 = "World";
str6.assign(anotherStr2); // 使用另一個std::string對象進行賦值
// 等價于 str6.assign(anotherStr2);std::string str7;
str7.assign(5, 'X'); // 使用5個字符'X'進行賦值
// 等價于 str7.assign("XXXXX", std::strlen("XXXXX"));
string字符串拼接
在字符串末尾拼接字符串
函數(shù)原型:
string& operator+=(const char* str);
//重載+=操作符string& operator+=(const char c);
//重載+=操作符string& operator+=(const string& str);
//重載+=操作符string& append(const char *s);
//把字符串s連接到當(dāng)前字符串結(jié)尾string& append(const char *s, int n);
//把字符串s的前n個字符連接到當(dāng)前字符串結(jié)尾string& append(const string &s);
// 同 operator+=(const string& str)string& append(const string &s, int pos, int n);
//字符串s中從pos開始的n個字符連接到字符串結(jié)尾
示例
#include <iostream>
#include <string>int main() {std::string str = "Hello";// 使用 operator+= 將 C 風(fēng)格字符串拼接到 str 的末尾str += " World";std::cout << str << std::endl; // 輸出:Hello World// 使用 append 將字符串的前 n 個字符拼接到 str 的末尾std::string s1 = "Welcome";str.append(s1, 3, 4);std::cout << str << std::endl; // 輸出:Hello Worldcome// 使用 operator+= 將另一個 std::string 對象拼接到 str 的末尾std::string s2 = "!";str += s2;std::cout << str << std::endl; // 輸出:Hello Worldcome!return 0;
}
string查找和替換
查找:查找指定字符串是否存在
替換:在指定的位置替換字符串
函數(shù)原型
-
int find(const string& str, int pos = 0) const;
//查找str第一次出現(xiàn)位置,從pos開始查找 -
int find(const char* s, int pos = 0) const;
//查找s第一次出現(xiàn)位置,從pos開始查找 -
int find(const char* s, int pos, int n) const;
//從pos位置查找s的前n個字符第一次位置 -
int find(const char c, int pos = 0) const;
//查找字符c第一次出現(xiàn)位置 -
int rfind(const string& str, int pos = npos) const;
//查找str最后一次位置,從pos開始查找 -
int rfind(const char* s, int pos = npos) const;
//查找s最后一次出現(xiàn)位置,從pos開始查找 -
int rfind(const char* s, int pos, int n) const;
//從pos查找s的前n個字符最后一次位置 -
int rfind(const char c, int pos = 0) const;
//查找字符c最后一次出現(xiàn)位置 -
string& replace(int pos, int n, const string& str);
//替換從pos開始n個字符為字符串str -
string& replace(int pos, int n,const char* s);
//替換從pos開始的n個字符為字符串s
注意:函數(shù)聲明中的
const
關(guān)鍵字表示該成員函數(shù)不會修改類的成員變量
示例
#include <iostream>
#include <string>int main() {std::string str = "Hello World";// 使用 find 函數(shù)查找字符串是否存在int pos1 = str.find("World");if (pos1 != std::string::npos) {std::cout << "Found at position: " << pos1 << std::endl;} else {std::cout << "Not found" << std::endl;}// 使用 rfind 函數(shù)從后向前查找字符串最后一次出現(xiàn)的位置int pos2 = str.rfind("l");if (pos2 != std::string::npos) {std::cout << "Last found at position: " << pos2 << std::endl;} else {std::cout << "Not found" << std::endl;}// 使用 replace 函數(shù)替換指定位置的字符串std::string repl = "Everyone";str.replace(6, 5, repl);std::cout << "After replacement: " << str << std::endl;return 0;
}
string字符串比較
字符串之間的比較
比較方式 字符串比較是按字符的ASCII碼進行對比
= 返回 0
> 返回 1
< 返回 -1
函數(shù)原型
int compare(const string &s) const;
//與字符串s比較int compare(const char *s) const;
//與字符串s比較
示例
#include <iostream>
#include <string>int main() {std::string str1 = "Hello";std::string str2 = "World";// 使用 compare 函數(shù)比較兩個字符串int result1 = str1.compare(str2);if (result1 < 0) {std::cout << str1 << " is less than " << str2 << std::endl;} else if (result1 > 0) {std::cout << str1 << " is greater than " << str2 << std::endl;} else {std::cout << str1 << " is equal to " << str2 << std::endl;}// 使用 compare 函數(shù)比較字符串和字符數(shù)組const char* str3 = "Hello";int result2 = str1.compare(str3);if (result2 < 0) {std::cout << str1 << " is less than " << str3 << std::endl;} else if (result2 > 0) {std::cout << str1 << " is greater than " << str3 << std::endl;} else {std::cout << str1 << " is equal to " << str3 << std::endl;}return 0;
}
string字符存取
string中單個字符存取方式有兩種
char& operator[](int n);
//通過[]方式取字符char& at(int n);
//通過at方法獲取字符
使用下標(biāo)([]
)運算符
可以使用下標(biāo)運算符來直接訪問字符串中的單個字符。下標(biāo)從0開始,表示第一個字符,依次遞增。
例如,str[0]
表示字符串str
中的第一個字符。
示例
#include <iostream>
#include <string>int main() {std::string str = "Hello, World!";// 使用下標(biāo)運算符訪問單個字符char ch1 = str[0]; // 獲取字符串的第一個字符char ch2 = str[7]; // 獲取字符串的第八個字符std::cout << "ch1: " << ch1 << std::endl;std::cout << "ch2: " << ch2 << std::endl;return 0;
}
在上述示例中,我們創(chuàng)建了一個字符串對象
str
,將其初始化為"Hello, World!"
。然后,使用下標(biāo)運算符[]
獲取字符串中的第一個字符和第八個字符,并分別將它們存儲在變量ch1
和ch2
中。最后,我們使用std::cout
輸出這兩個字符。
使用at()
函數(shù)
另一種訪問單個字符的方式是使用
at()
函數(shù)。at()
函數(shù)與下標(biāo)運算符類似,可以用來訪問指定位置的字符。與下標(biāo)運算符不同的是,at()
函數(shù)會進行邊界檢查,如果訪問位置超出字符串的范圍,會拋出一個std::out_of_range
異常。
示例
#include <iostream>
#include <string>int main() {std::string str = "Hello, World!";// 使用at()函數(shù)訪問單個字符char ch1 = str.at(0); // 獲取字符串的第一個字符char ch2 = str.at(7); // 獲取字符串的第八個字符std::cout << "ch1: " << ch1 << std::endl;std::cout << "ch2: " << ch2 << std::endl;return 0;
}
在上述示例中,我們使用at()
函數(shù)來獲取字符串中的第一個字符和第八個字符,并將它們分別存儲在變量ch1
和ch2
中。然后,我們使用std::cout
輸出這兩個字符。
無論是使用下標(biāo)運算符還是
at()
函數(shù),都可以用來訪問和修改字符串中的單個字符。但需要注意的是,如果使用下標(biāo)運算符訪問超出字符串范圍的位置,程序可能會發(fā)生未定義行為,因此在訪問字符之前最好先檢查字符串的長度。
string插入和刪除
對string字符串進行插入和刪除字符操作
函數(shù)原型
string& insert(int pos, const char* s);
//插入字符串string& insert(int pos, const string& str);
//插入字符串string& insert(int pos, int n, char c);
//在指定位置插入n個字符cstring& erase(int pos, int n = npos);
//刪除從Pos開始的n個字符
示例
#include <iostream>
#include <string>int main() {std::string str = "Hello";// 使用 insert 在指定位置插入字符串str.insert(1, "123");std::cout << str << std::endl; // 輸出:H123ello// 使用 erase 刪除指定位置的字符str.erase(2, 3);std::cout << str << std::endl; // 輸出:H1elloreturn 0;
}
該示例演示了如何對字符串進行插入和刪除字符操作。使用 insert
函數(shù)可以在指定位置插入字符串,這里在位置1插入字符串"123",結(jié)果為"H123ello"。使用 erase
函數(shù)可以刪除指定位置的字符,這里刪除位置2開始的3個字符,結(jié)果為"H1ello"。
string子串
從字符串中獲取想要的子串
函數(shù)原型
string substr(int pos = 0, int n = npos) const;
//返回由pos開始的n個字符組成的字符串
示例
#include <iostream>
#include <string>int main() {std::string str = "Hello, World!";// 使用 substr 獲取子串std::string sub1 = str.substr(7); // 從位置7開始直到字符串末尾的子串std::cout << sub1 << std::endl; // 輸出:World!std::string sub2 = str.substr(7, 5); // 從位置7開始的5個字符組成的子串std::cout << sub2 << std::endl; // 輸出:Worldreturn 0;
}
該示例演示了如何從字符串中獲取子串。使用 substr 函數(shù)可以根據(jù)指定的起始位置和長度獲取子串。在示例中,通過 str.substr(7) 獲取從位置7開始直到字符串末尾的子串,結(jié)果為"World!“。通過 str.substr(7, 5)
獲取從位置7開始長度為5的子串,結(jié)果為"World”。