記事のディレクトリ
3.1文字列コンテナ
3.1.1文字列の基本概念
自然:
- stringはC ++スタイルの文字列であり、stringは基本的にクラスです。
stringとcharの違い*:
- char *はポインタです
- Stringは、char *をカプセル化するクラスであり、char *タイプのコンテナーであるこの文字列を管理します。
特徴:
多くのメンバーメソッドは文字列クラス内にカプセル化されています
例:検索検索、コピーコピー、削除削除置換置換、挿入挿入
Stringは、char *によって割り当てられたメモリを管理します。範囲外のコピーや範囲外の値などについて心配する必要はありません。これは、クラスの責任です。
3.1.2文字列コンストラクタ
コンストラクターのプロトタイプ:
string();
//空の文字列を作成します例:string str;
string(const char* s);
// stringsで初期化しますstring(const string& str);
//文字列オブジェクトを使用して別の文字列オブジェクトを初期化しますstring(int n, char c);
// n文字cを使用して初期化します
例:
#include <string>
//string构造
void test01()
{
string s1; //创建空字符串,调用无参构造函数
cout << "str1 = " << s1 << endl;
const char* str = "hello world";
string s2(str); //把c_string转换成了string
cout << "str2 = " << s2 << endl;
string s3(s2); //调用拷贝构造函数
cout << "str3 = " << s3 << endl;
string s4(10, 'a');
cout << "str3 = " << s3 << endl;
}
int main() {
test01();
system("pause");
return 0;
}
概要:文字列の複数の構築方法は比較できず、柔軟に使用できます。
3.1.3文字列代入演算
機能の説明:
- 文字列に値を割り当てる
割り当てのための関数プロトタイプ:
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を使用して、現在の文字列を割り当てます
例:
//赋值
void test01()
{
string str1;
str1 = "hello world";
cout << "str1 = " << str1 << endl;
string str2;
str2 = str1;
cout << "str2 = " << str2 << endl;
string str3;
str3 = 'a';
cout << "str3 = " << str3 << endl;
string str4;
str4.assign("hello c++");
cout << "str4 = " << str4 << endl;
string str5;
str5.assign("hello c++",5);
cout << "str5 = " << str5 << endl;
string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl;
string str7;
str7.assign(5, 'x');
cout << "str7 = " << str7 << endl;
}
int main() {
test01();
system("pause");
return 0;
}
総括する:
文字列値operator=
を割り当てる方法はたくさんありますが、この方法の方が実用的です
3.1.4文字列文字列の連結
機能の説明:
- 文字列の最後でスプライシング文字列を実現します
関数プロトタイプ:
string& operator+=(const char* str);
//オーバーロード+ =演算子string& operator+=(const char c);
//オーバーロード+ =演算子string& operator+=(const string& str);
//オーバーロード+ =演算子string& append(const char *s);
//文字列sを現在の文字列の末尾に接続しますstring& append(const char *s, int n);
//文字列sの最初のn文字を現在の文字列の末尾に接続しますstring& append(const string &s);
//同演算子+ =(const string&str)string& append(const string &s, int pos, int n);
//文字列sのposからのn文字は、文字列の末尾に接続されます
例:
//字符串拼接
void test01()
{
string str1 = "我";
str1 += "爱玩游戏";
cout << "str1 = " << str1 << endl;
str1 += ':';
cout << "str1 = " << str1 << endl;
string str2 = "LOL DNF";
str1 += str2;
cout << "str1 = " << str1 << endl;
string str3 = "I";
str3.append(" love ");
str3.append("game abcde", 4);
//str3.append(str2);
str3.append(str2, 4, 3); // 从下标4位置开始 ,截取3个字符,拼接到字符串末尾
cout << "str3 = " << str3 << endl;
}
int main() {
test01();
system("pause");
return 0;
}
概要:文字列スプライシングには多くのオーバーロードバージョンがありますが、初心者の段階でいくつか覚えておいてください
3.1.5文字列の検索と置換
機能の説明:
- 検索:指定された文字列が存在するかどうかを検索します
- 置換:指定された位置で文字列を置換します
関数プロトタイプ:
int find(const string& str, int pos = 0) const;
// posから始めて、strの最初の出現位置を見つけますint find(const char* s, int pos = 0) const;
// posから始めて、sの最初の出現位置を見つけますint find(const char* s, int pos, int n) const;
//位置からsの最初のn文字の最初の位置を見つけますint find(const char c, int pos = 0) const;
//文字cの最初の出現位置を見つけますint rfind(const string& str, int pos = npos) const;
// posから始めて、strの最後の位置を見つけます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の最後の出現位置を検索しますstring& replace(int pos, int n, const string& str);
// n文字をposからstringstrに置き換えますstring& replace(int pos, int n,const char* s);
// n文字をposからstringsに置き換えます
例:
//查找和替换
void test01()
{
//查找
string str1 = "abcdefgde";
int pos = str1.find("de");
if (pos == -1)
{
cout << "未找到" << endl;
}
else
{
cout << "pos = " << pos << endl;
}
pos = str1.rfind("de");
cout << "pos = " << pos << endl;
}
void test02()
{
//替换
string str1 = "abcdefgde";
str1.replace(1, 3, "1111");
cout << "str1 = " << str1 << endl;
}
int main() {
//test01();
//test02();
system("pause");
return 0;
}
総括する:
- 検索は左から後ろへ、rfindは右から左へ
- 検索は、文字列を検索した後の最初の文字位置を返し、見つからない場合は-1を返します。
- 置き換えるときは、どの位置から、何文字、どの種類の文字列を置き換えるかを指定する必要があります
3.1.6文字列文字列の比較
機能の説明:
- 文字列間の比較
比較方法:
- 文字列の比較は、文字のASCIIコードを比較することです
= 0を返す
>リターン1
<リターン-1
関数プロトタイプ:
int compare(const string &s) const;
//文字列と比較しますint compare(const char *s) const;
//文字列と比較します
例:
//字符串比较
void test01()
{
string s1 = "hello";
string s2 = "aello";
int ret = s1.compare(s2);
if (ret == 0) {
cout << "s1 等于 s2" << endl;
}
else if (ret > 0)
{
cout << "s1 大于 s2" << endl;
}
else
{
cout << "s1 小于 s2" << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
概要:文字列の比較は、主に2つの文字列が等しいかどうかを比較するために使用されます。どちらが大きいか、誰が小さいかを判断することはあまり意味がありません。
3.1.7文字列アクセス
文字列内の1文字にアクセスする方法は2つあります
char& operator[](int n);
// []を介して文字をフェッチしますchar& at(int n);
// atメソッドを介して文字を取得します
例:
void test01()
{
string str = "hello world";
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << " ";
}
cout << endl;
for (int i = 0; i < str.size(); i++)
{
cout << str.at(i) << " ";
}
cout << endl;
//字符修改
str[0] = 'x';
str.at(1) = 'x';
cout << str << endl;
}
int main() {
test01();
system("pause");
return 0;
}
概要:文字列内の1つの文字にアクセスするには、[]またはatを使用する2つの方法があります。
3.1.8文字列の挿入と削除
機能の説明:
- 文字列の文字を挿入および削除します
関数プロトタイプ:
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);
//位置から始まるn文字を削除します
例:
//字符串插入和删除
void test01()
{
string str = "hello";
str.insert(1, "111");
cout << str << endl;
str.erase(1, 3); //从1号位置开始3个字符
cout << str << endl;
}
int main() {
test01();
system("pause");
return 0;
}
**概要:**挿入と削除の最初の添え字は0から始まります
3.1.9文字列部分文字列
機能の説明:
- 文字列から目的の部分文字列を取得します
関数プロトタイプ:
string substr(int pos = 0, int n = npos) const;
// posで始まるn文字で構成される文字列を返します
例:
//子串
void test01()
{
string str = "abcdefg";
string subStr = str.substr(1, 3);
cout << "subStr = " << subStr << endl;
string email = "[email protected]";
int pos = email.find("@");
string username = email.substr(0, pos);
cout << "username: " << username << endl;
}
int main() {
test01();
system("pause");
return 0;
}
**概要:**部分文字列関数を柔軟に使用すると、実際の開発で効果的な情報を取得できます
詳細については、公式アカウントに従ってください。