1. string 容器
1.1 string概念
本質:
string是C++風格的字元串,而string本質上是一個類。
string和char * 差別:
char * 是一個指針 ,string是一個類,類内部封裝了char *,管理這個字元串,是一個char *型的容器。
string管理char*所配置設定的記憶體,不用擔心複制越界和取值越界等,由類内部進行負責。
1.2 構造函數
函數原型:
函數原型 | 功能 |
string(); | 建立一個空的字元串。 |
string(const char* s); | 使用字元串s初始化。 |
string(const string& str); | 使用一個string對象初始化另一個string對象。 |
string(int n, char c); | 使用n個字元c初始化。 |
代碼示例
#include <iostream>
#include<string>
using namespace std;
int main()
{
string s1; //建立一個空的字元串。
const char* str = "Hello C++"; // string(const char* s); 使用字元串s初始化。
string s2(str);
cout << s2 << endl;
string s3(s2); // 使用一個string對象初始化另一個string對象。
cout << s3 << endl;
string s4(10,'a'); //使用10個字元‘a’初始化
cout << s4 << endl;
}
1.3 string指派操作
函數原型:
函數原型 | 功能 |
string& operator=(const char* s); | char*類型字元串指派給目前的字元串。 |
string& operator=(const string &s); | 把字元串s賦給目前的字元串。 |
string& operator=(char c); | 字元指派給目前的字元串。 |
string& assign(const char *s); | 把字元串s賦給目前的字元串。 |
string& assign(const char *s, int n); | 把字元串s的前n個字元賦給目前的字元串 |
string& assign(const string &s); | 把字元串s賦給目前字元串。 |
string& assign(int n, char c); | 用n個字元c賦給目前字元串。 |
代碼示例
#include <iostream>
#include<string>
using namespace std;
int main()
{
string str1;
str1 = "Hello C++";
cout << str1 << endl;
string str2;
str2 = str1;
cout << str2 << endl; // 把字元串s賦給目前的字元串
string str3;
str3 = 'a';
cout << str3 << endl; //字元指派給目前的字元串
/* string str4;
str4.assign("Hello C++");
cout << str4 << endl; // 把字元串s賦給目前的字元串
*/ // 個人認為不如直接 str4 = "Hello C++";
string str5;
str5.assign("Hello C++", 4);
cout << str5 << endl; //把字元串s的前n個字元賦給目前的字元串 注意是賦給
// 不能寫成 str5.assign(str1, 4); 原因 見函數原型
//函數原型 string& assign(const char *s, int n);
// 與下面的append 拼接做好區分
/* string str6;
str6.assign(str5); //把字元串s賦給目前字元串
cout << str6 << endl; //這裡建議 str6 = str5; 比較簡便
*/
string str7;
str7.assign(10,'w');
cout << str7 << endl; //用n個字元c賦給目前字元串
}
1.4 string字元串拼接
實作字元串末尾拼接字元串的函數原型:
函數原型 | |
string& operator+=(const char* str); | 重載+=操作符。 |
string& operator+=(const char c); | 重載+=操作符。 |
tring& operator+=(const string& str); | 重載+=操作符。 |
string& append(const char *s); | 把字元串s連接配接到目前字元串結尾。 |
string& append(const char *s, int n); | 把字元串s的前n個字元連接配接到目前字元串結尾。 |
string& append(const string &s); | 把字元串s連接配接到目前字元串結尾。 |
string& append(const string &s, int pos, int n); | 字元串s中從pos開始的n個字元連接配接到字元串結尾。 |
示例:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1="歡迎";
s1+="學習"; //重載+=操作符
cout<<s1<<endl;
s1+=':'; //重載+=操作符
cout<<s1<<endl;
string s2="STL";
s1+=s2; // 重載+=操作符 s1="歡迎學習:STL";
cout<<s1<<endl;
string s3="I ";
s3.append("want to"); // 把字元串s連接配接到目前字元串結尾
cout<<s3<<endl;
s3.append(" be an acmer abcd",12); // 把字元串s的前n個字元連接配接到目前字元串結尾 空格也算
cout<<s3<<endl;
s3.append(s1); //把字元串s連接配接到目前字元串結尾
cout<<s3<<endl;
s3.append(s2,0,3); // 字元串s中從pos開始的n個字元連接配接到字元串結尾 從第0位開始數
cout<<s3<<endl;
}
1.5 string查找和替換
函數原型:
函數模型 | 功能 |
int find(const string& str, int pos = 0) const; | 查找str第一次出現位置,從pos開始查找。 |
int find(const char* s, int pos = 0) const; | 查找s第一次出現位置,從pos開始查找。 |
int find(const char* s, int pos, int n) const; | 從pos位置查找s的前n個字元第一次位置。 |
int find(const char c, int pos = 0) const; | 查找字元c第一次出現位置。 |
int rfind(const string& str, int pos = npos) const; | 查找str最後一次位置,從pos開始查找。 |
int rfind(const char* s, int pos = npos) const; | 查找s最後一次出現位置,從pos開始查找。 |
int rfind(const char* s, int pos, int n) const; | 從pos查找s的前n個字元最後一次位置。 |
int rfind(const char c, int pos = 0) const; | 查找字元c最後一次出現位置。 |
示例:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1="hello acm acm";
int pos1 =s1.find("cm"); //未找到即傳回-1
cout<<pos1<<endl;
int pos2=s1.find("cm",7,2); //從pos (1) 位置查找s (cm) 的前n (1) 個字元第一次位置。
cout<<pos2<<endl;
int pos3=s1.rfind("acm"); //查找s最後一次出現位置,從pos開始查找。
cout<<pos3<<endl;
string s2="acm";
int pos4=s1.rfind(s2); //查找str最後一次位置,從pos開始查找。
cout<<pos4<<endl;
}
在指定的位置替換字元串的函數模型:
函數原型 | 功能 |
string& replace(int pos, int n, const string& str); | 替換從pos開始n個字元為字元串str。 |
string& replace(int pos, int n,const char* s); | 替換從pos開始的n個字元為字元串s。 |
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1="abcdef";
string s2;
s2=s1.replace(0,3,"1111");//從0開始的三個字元替換第0位也算1一個數 1111def
cout<<s2<<endl;
]
1.6 string字元串比較
比較字元串大小的函數模型:
函數模型 | 功能 |
int compare(const string &s) const; | 與字元串s比較。 |
int compare(const char *s) const; | 與字元串s比較。 |
示例:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1 = "hello";
string s2 = "hello";
//與字元串s比較
if (s1.compare(s2) == 0)
cout << "=" << endl;
else if (s1.compare(s2) > 0)
cout << ">" << endl;
else cout << "<" << endl;
}
1.7 string字元存取
string中單個字元存取的函數模型:
下标操作符 [] 在使用時不檢查索引的有效性,如果下标超出字元的長度範圍,會示導緻未定義行為。
函數 at() 在使用時會檢查下标是否有效,并給出提示 out_of_range 異常
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1="succes need patient";
for(i=0;i<s1.size();i++)
{
cout <<s1[i]<<endl; // cout<<s1.at[i]<<endl;
}
cout<<endl;
s1[0]='S'; //s1.at[0]='S';
cout<<s1<<endl;
char c=s1[0]; //char c=s.at[0]
cout<<c<<endl;
}
1.8 string插入和删除
對string字元串進行插入和删除字元操作的函數原型:
函數模型 | 功能 |
string& insert(int pos, const char* s); | 插入字元串。 |
string& insert(int pos, const string& str); | 插入字元串。 |
string& insert(int pos, int n, char c); | 在指定位置插入n個字元c。 |
string& erase(int pos, int n = npos); | 删除從Pos開始的n個字元。 |
示例:
插入和删除的起始下标都是從0開始
。
#include <iostream>
#include<string>
using namespace std;
int main()
{
string str = "hello";
str.insert(1,"111"); //插入字元串 在e前面插入111 即h111ello
cout << str << endl;
str.erase(1,3);// 删除從Pos開始的n個字元
cout << str << endl;
str.insert(1,5,'1');// 插入從Pos開始的n個字元 即h11111ello
cout << str << endl;
}
1.9 string子串
函數模型 | 功能 |
string substr(int pos = 0, int n = npos) const; | 傳回由pos開始的n個字元組成的字元串。 |
示例:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1="icpc acm";
string s2=s1.substr(4,4); // 編号從0開始 從4号即空格開始 取4個
cout<<s2<<endl; // 輸出 acm ac前有空格
//配合 find 實作對某種格式擷取
string s3="[email protected]";
int pos=s3.find('@');
string s4=s3.substr(0,pos);
cout<<s4<<endl;
}