文字列コンテナの使用法

文字列コンテナの使用法

文字列コンテナの概要

本質:文字列はC ++スタイルの文字列であり、文字列は基本的にクラスです。

stringとcharの違い*:

①char*はポインタです。

②文字列はクラスです。Char*はクラス内にカプセル化されています。この文字列を管理するためのchar *タイプのコンテナです。

文字列コンテナの初期化

#include <iostream>  
using namespace std;  
#include <string>  
  
int main()  
{  
    const char ch[] = "6sjkasa";  
    string str(ch); // 用字符型数组进行初始化  
    string str1(str1); // 调用string容器的拷贝构造函数初始化  
    string str2("skajda"); // 调用普通构造函数进行初始化  
    string str4(4, 'a'); // 用字符常量初始化字符串的正确方法
} 

 

なぜコピーコンストラクターを呼び出すのですか?

渡す金額が可変の場合、次の手順が必要です。

 

文字列代入演算

#include <iostream>  
using namespace std;  
#include <string>  
  
int main()  
{  
    const char ch[] = "6sjkasa";  
    string str(ch); // 用字符型数组进行初始化  
    string str1(str); // 调用string容器的拷贝构造函数初始化  
    string str2("skajda"); // 调用普通构造函数进行初始化  
    string str3(4, 'a'); // 用字符常量初始化字符串的正确方法  
  
    string str4;  
    const char ch1[] = "saskjsk", ch2 = 'a';  
    str4 = ch1; // 用字符型数组赋值字符串  
    cout << str4 << endl;  
    str4 = ch2; // 用字符赋值字符串  
    cout << str4 << endl;  
    str4 = str2; // 用已有字符串赋值字符串  
    cout << str4 << endl;  
    str4.assign(str1); // 用assign成员函数进行字符串的赋值操作  
    cout << str4 << endl;  
    str4.assign(ch1,5); // 用assign成员函数进行字符串的赋值操作(将字符数组的前5个字符赋值给string对象)  
    cout << str4 << endl;  
    str4.assign(ch1); // 用assign成员函数进行字符串的赋值操作  
    cout << str4 << endl;  
    str4.assign(3,'f'); // 用assign成员函数进行字符串的赋值操作  
    cout << str4 << endl;  
}  

 

文字列の連結

#include <iostream>  
using namespace std;  
#include <string>  
  
int main()  
{  
    string str1 = "shisishd";  
    string str2(1, 'd');  
    str2 += str1; // 在字符串尾部拼接字符串  
    cout << str2 << endl;  
    const char ch1[] = "666", ch2 = 'p';  
    str2 += ch1; // 字符串尾部拼接字符型数组  
    cout << str2 << endl;  
    str2 += ch2; // 字符串尾部拼接字符常量  
    cout << str2 << endl;   
    str2.append(ch1, 2); // 把字符型数组的前2个字符拼接到str2字符串的结尾  
    cout << str2 << endl;  
    str2.append(str1, 4, 3); // 将字符串str1的4-7字符拼接至str2字符串结尾  
    cout << str2 << endl;  
    str2.append(ch1); // 把字符型数组整个拼接至str2字符串的结尾  
    cout << str2 << endl;  
    str2.append(str1); // 把字符串str1整个拼接至str2字符串结尾  
    cout << str2 << endl;  
    str2.append(4, 'e'); // 把4个字符'e'拼接至str2字符串的结尾  
    cout << str2 << endl;  
}  

 

文字列を見つけて置き換える

#include <iostream>  
#include <string>  
using namespace std;  
  
int main()  
{  
    string str("sjdjdshgjda"), str1("jd");  
    int pos = str.find(str1, 2);  
    cout << pos << endl;  // pos = 3
}  

 

ここでは、3つの「jd」文字列があることがわかりますが、2番目から左から右に開始して最初に検索するため、「jd」が3番目の文字に初めて表示されます。

対応する文字が見つからない限り、Find関数またはrfind関数のいずれかが-1を返し、指定された文字が見つからないことを示します。

#include <iostream>  
#include <string>  
using namespace std;  
  
int main()  
{  
    string str("sjdjdshgjda"), str1("jd");  
    int pos = str.rfind(str1, 10);  
    cout << pos << endl;  // 8
}  

 

#include <iostream>  
#include <string>  
using namespace std;  
  
int main()  
{  
    string str("sjdjdshgjda"), str1("jd");  
    int pos = str.rfind(str1, 10);  
    cout << pos << endl;  
  
    str.replace(3, 2, "666"); // index=3-5的字符替换为"666"  
    pos = str.find("666", 0); // 从str第0个字符开始查找字符串"666"  
    str.replace(pos, sizeof("666"), "777");  
    cout << str << endl;  
}  

 

上記は、検索機能と置換機能を組み合わせて使用​​する場合です。

 

上記のオーバーロードされた関数タイプは、プログラム例の使用法に似ているため、ここでは繰り返しません。

文字列コンテナの比較

#include <iostream>  
#include <string>  
using namespace std;  
  
int main()  
{  
    string str("sjahjdhja"), str1("654sajdhadhj");  
    int compare = str.compare(str1);  
    if (compare > 0)  
    {  
        cout << "str>str1" << endl;  
    }  
    else if(compare == 0)  
    {  
        cout << "str==str1" << endl;  
    }  
    else  
    {  
        cout << "str<str1" << endl;  
    }  
}  

 

もちろん、比較メンバー関数の最初のパラメーターを「文字配列」に変更することもできます。比較関数のプロトタイプは次のとおりです。

 

文字列コンテナへのアクセス

#include <iostream>  
using namespace std;  
#include <string>  
  
int main()  
{  
    string str = "shajhajhsjash";  
    cout << str[2] << endl;  // 只读
    str[2] = 'p';  // 写
    cout << str[2] << endl;  
    cout << str.at(4) << endl;  // 只读
    str.at(4) = 'o';  // 写
    cout << str.at(4) << endl;  
}  

 

[]を使用して、配列のように文字列内のchar型要素にアクセスできることがわかります。また、組み込みのatメンバー関数を使用して、文字列クラス内のchar型文字要素にアクセスできます。また、このアクセス方法を使用して、特定の文字を読み書きします。

文字列コンテナの挿入と削除

#include <iostream>  
using namespace std;  
#include <string>  
  
int main()  
{  
    string str("666777888999"),str1("555");  
    str.insert(3, 3, 'a'); // 在3字符位置插入3个'a'字符  
    str.insert(0, str1); // 在0字符位置插入str1字符串  
    const char ch[] = "sjahja";  
    str.insert(0, ch, 2); // 在0字符位置插入ch字符型数组的前两个元素  
    cout << str << endl;  
  
    cout << str.find('6', 0) << endl;  
    cout << str.rfind('6', str.size()) << endl;  
    str.erase(str.find('6', 0), str.rfind('6', str.size()) - str.find('6', 0) + 1); // 字符串与find/rfind查找函数搭配使用  
    cout << str << endl;  
}  

 

注:ここでは、通常、find / rfind検索関数を挿入/削除関数と一緒に使用します。検索するrfind関数の使用法と、その戻り値を理解する必要があります。

文字列の部分文字列の抽出

#include <iostream>  
using namespace std;  
#include <string>  
  
int main()  
{  
    string str("666777888999"),str1("555");  
    str.insert(3, 3, 'a'); // 在3字符位置插入3个'a'字符  
    str.insert(0, str1); // 在0字符位置插入str1字符串  
    const char ch[] = "sjahja";  
    str.insert(0, ch, 2); // 在0字符位置插入ch字符型数组的前两个元素  
    cout << str << endl;  
  
    cout << str.find('6', 0) << endl;  
    cout << str.rfind('6', str.size()) << endl;  
    str.erase(str.find('6', 0), str.rfind('6', str.size()) - str.find('6', 0) + 1); // 字符串与find/rfind查找函数搭配使用  
    cout << str << endl;  
  
    string str2;  
    str2 = str.substr(str.find('9', 0), str.rfind('9', str.size()) - str.find('9', 0) + 1);   
    // 我们用提取字串的函数搭配find/rfind函数使用就可以提取出我们想要的一段字符  
    cout << str2 << endl;  
}  

 

通常、部分文字列を抽出する場合は、find / rfind検索関数を使用して、必要な文字を抽出できるようにします。

おすすめ

転載: blog.csdn.net/weixin_45590473/article/details/110452785