C++提高程式設計---3.1 常用容器-string 容器【P189~P196】
-
- 3.1 string 容器
-
- 3.1.1 string 基本概念
- 3.1.2 string 構造函數
- 3.1.3 string 指派操作
- 3.1.4 string 字元串拼接
- 3.1.5 string 查找和替換
- 3.1.6 string 字元串比較
- 3.1.7 string 字元存取
- 3.1.8 string 字元插入和删除
- 3.1.9 string 子串
3.1 string 容器
3.1.1 string 基本概念
本質:
- string是C++風格的字元串,而string本質上是一個類
string和char*差別:
- char*是一個指針
- string是一個類,類内部封裝了char*,管理這個字元串,是一個char*型的容器。
特點:
string 類内部封裝了很多成員方法
例如︰查找 find,拷貝 copy,删除 delete 替換 replace,插入 insert
string 管理 char* 所配置設定的記憶體,不用擔心複制越界和取值越界等,由類内部進行負責
3.1.2 string 構造函數
構造函數原型:
-
// 建立一個空的字元串 例如:string strstring();
-
// 使用字元串 s 初始化string(const char *s);
-
// 使用一個 string 對象初始化另一個 string 對象string(const string& str);
-
// 使用 n 個字元 c 初始化string(int n, char c);
# include<iostream>
# include<string>
using namespace std;
void test01()
{
string s1; // 預設構造
// C語言風格的字元串
const char * str = "hello world";
string s2(str);
cout << "s2 = " << s2 << endl;
string s3(s2);
cout << "s3 = " << s3 << endl;
string s4(10, 'a');
cout << "s4 = " << s4 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.1.3 string 指派操作
構造函數是對對象進行初始化操作,那麼初始化過後可以對其進行新的指派操作
功能描述:
- 給 string 字元串進行指派
指派的函數原型:
-
//char* 類型字元串 指派給目前的字元串string& operator=(const char* s);
-
//把字元串 s 指派給目前的字元串string& operator=(const string &s);
-
//字元指派給目前的字元串string& operator=(char c);
-
//把字元串 s 指派給目前的字元串string& assign(const char *s);
-
//把字元串的前 n 個字元指派給目前的字元串string& assign(const char *s, int n);
-
//把字元串 s 指派給目前字元串string& assign(const string &s);
-
//用 n 個字元c 指派給目前字元串string& assign(int n, char c);
# include<iostream>
# include<string>
using namespace std;
void test01()
{
// 1
string str1;
str1 = "hello world";
cout << "str1 = " << str1 << endl;
// 2
string str2;
str2 = str1;
cout << "str2 = " << str2 << endl;
// 3
string str3;
str3 = 'a';
cout << "str3 = " << str3 << endl;
// 4
string str4;
str4.assign("hello C++");
cout << "str4 = " << str4 << endl;
// 5
string str5;
str5.assign("hello C++", 5);
cout << "str5 = " << str5 << endl;
// 6
string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl;
// 7
string str7;
str7.assign(10, 'b');
cout << "str7 = " << str7 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.1.4 string 字元串拼接
# include<iostream>
# include<string>
using namespace std;
void test01()
{
// 1
string str1 = "我";
str1 += "愛玩遊戲";
cout << "str1 = " << str1 << endl;
// 2
str1 += ':';
cout << "str1 = " << str1 << endl;
// 3
string str2 = " LOL NaN";
str1 += str2;
cout << "str1 = " << str1 << endl;
// 4
string str3 = "I";
str3.append(" love");
cout << "str3 = " << str3 << endl;
// 5
str3.append(" game aaaaaaa", 5);
cout << "str3 = " << str3 << endl;
// 6
str3.append(str2);
cout << "str3 = " << str3 << endl;
// 7
string str4 = "I love game:";
str4.append(str2, 0, 4);
cout << "str4 = " << str4 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.1.5 string 查找和替換
查找
find 和 rfind 差別:
find 是從左往右找,rfind 是從右往左找
# include<iostream>
using namespace std;
// 字元串查找和替換
// 1、查找
void test01()
{
// find
string str1 = "abcdefghijklmnopqrstuvwxyzde";
int pos1 = str1.find("ed", 0);
cout << "如果字元串中沒有所要查找的相應字元,那麼傳回的 pos 值為:" << pos1 << endl;
int pos = str1.find("de", 0);
if (pos == -1)
{
cout << "未找到該字元串" << endl;
}
else
{
cout << "字元串 'de' 所在位置的下标 pos 為:" << pos << endl;
}
// rfind
int pos2 = str1.rfind("de");
cout << "pos2 = " << pos2 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
替換
# include<iostream>
# include<string>
using namespace std;
// 字元串查找和替換
// 替換
void test02()
{
string str4 = "abcdefgh";
str4.replace(1, 3, "1111"); // 從1号位置起3個字元替換為"1111"
cout << "str4 = " << str4 << endl;
}
int main()
{
test02();
system("pause");
return 0;
}
3.1.6 string 字元串比較
# include<iostream>
using namespace std;
void test01()
{
string str1 = "hello";
string str2 = "xello";
if(str1.compare(str2) == 0)
{
cout << "str1 等于 str2" << endl;
}
else if (str1.compare(str2) == 1)
{
cout << "str1 大于 str2" << endl;
}
else
{
cout << "str1 小于 str2" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
字元串對比主要是用于比較兩個字元串是否相等,判斷誰大誰小的意義并不大
3.1.7 string 字元存取
string 中單個字元存取方式有兩種
-
// 通過[]方式取字元char& operator[](int n);
-
//通過 at 方式擷取字元char& at(int n);
# include<iostream>
# include<string>
using namespace std;
void test01()
{
string str1 = "hello";
cout << "str1 = " << str1 << endl;
// 1、通過[]通路單個字元
for (int i = 0; i < str1.size(); i++)
{
cout << str1[i] << " ";
}
cout << endl;
//2、通過 at 方式通路單個字元
for (int i = 0; i < str1.size(); i++)
{
cout << str1.at(i) << " ";
}
cout << endl;
// 不僅可以讀,還可以寫入單個字元
str1[0] = 'x';
cout << "str1 = " << str1 << endl;
str1.at(1) = 'x';
cout << "str1 = " << str1 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.1.8 string 字元插入和删除
# include<iostream>
# include<string>
using namespace std;
void test01()
{
string str1 = "hello";
// 插入
str1.insert(1, "2222222");
cout << "str1 = " << str1 << endl;
// 删除
str1.erase(1, 7);
cout << "str1 = " << str1 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
總結:插入和删除的起始下标都是從零開始
3.1.9 string 子串
功能描述
- 從字元串中擷取想要的子串
函數原型
-
//傳回由 pos 開始的 n 個字元組成的字元串string substr(int pos = 0, int n = npos) const;
# include<iostream>
# include<string>
using namespace std;
void test01()
{
string str = "abcdefgh";
cout << "str = " << str << endl;
string subStr = str.substr(1, 3);
cout << "subStr = " << subStr << endl;
}
// 實用操作
void test02()
{
string email = "[email protected]";
int pos = email.find('@');
string username = email.substr(0, pos);
cout << "username = " << username << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}