C ++コンテナ文字列クラス

C ++コンテナ文字列クラス

1.文字列クラスの
コンストラクター1デフォルトの構築文字列s1;
2コピー構築文字列s2(s1);
3パラメーター構築文字列s3( "aaa");
4 2パラメーター構築文字列s4(3、 'c');

// string类的构造函数
    string s1;       //默认构造
	string s2(s1);   //拷贝构造
	string s3("aaa");    //有参构造
	string s4(3, 'c');    //两个参数有参构造
	cout << "字符串s1为:"<<s1 << endl;
	cout <<"字符串s2为:"<< s2 << endl;
	cout <<"字符串s3为:"<< s3 << endl;
	cout <<"字符串s4为:"<< s4 << endl;
字符串s1为:
字符串s2为:
字符串s3为:aaa
字符串s4为:ccc

2.文字列クラスの代入操作
1.string&assign(const char * s、int n);文字列sの最初のn個の文字列を現在の文字
列に割り当てます2. string&assign(const string&s、int start、int n);割り当て開始から文字列までのsのn文字

//string类的赋值操作
string s5;
	//string& assign(const char *s,int n);  把字符串s的前n个字符赋给当前的字符串
	s5.assign("asddfsdsgdsgd", 8);
	cout << "s5=" << s5 << endl;

	//string& assign(const string &s,int start,int n); 将s从start开始n个字符赋值给字符串(从0开始计算)
	string s6 = "asdfdgfaghd";
	string s7;
	s7.assign(s6, 4, 5);
	cout << "s7=" << s7 << endl;

出力:

s5=asddfsds
s7=dgfag

3.文字列ストレージ操作
1.添え字を介して文字を取得します[]
2。atを介して文字を取得します3.atと[]
の違い:[]は範囲外にアクセスして直接ハングしますが、範囲外にアクセスすると自動的に例外がスローされます

    string s = "hello world!";
	for (int i = 0; i < s.size(); i++)
	{
    
    
		cout << s[i] << endl;//通过[]获取字符
		cout << s.at(i) << endl;//通过at获取字符
	}
	try
	{
    
    
		//s[100];
		s.at(100);
	}
	catch (out_of_range &e)
	{
    
    
		cout << e.what() << endl;
	}
h
h
e
e
l
l
l
l
o
o


w
w
o
o
r
r
l
l
d
d
!
!
invalid string position

第4に、文字列スプライシング操作
s.append(args)は、sにargsを追加します。sへの参照を返します

    string str1 = "我";
	string str2 = "爱北京";
	str1 += str2;
	cout << "str1="<<str1 << endl;
	string str3 = "天安门";
	str1.append("故宫");
	cout << "新的str1为:" << str1 << endl;

出力:

str1=我爱北京
新的str1为:我爱北京故宫

5.文字列
1を検索して置換します。find()、部分文字列を検索して最初の位置を返します。部分文字列が見つからない場合は、-1を直接返します。
2. rfind()とrfindの違いは、「r」は「right」を表すことです。3。Replaceは
replacementを表します。posから始まるn文字を文字列として置き換えます
。s.replace(range、args)範囲内の文字を削除します。 sの場合、argsで指定された文字に置き換えます。範囲は、添え字と長さ、またはsへのイテレータのペアのいずれかです。sへの参照を返します

string str1 = "ABCDEFGHIJKDE";
	int pos1 = str1.find("DE");
	int pos2 = str1.rfind("DE");
	cout << "pos1=" << pos1 << endl;
	cout << "pos2=" << pos2 << endl;
	str1.replace(3, 4, "126");
	cout << "str1"<<str1 << endl;

出力:

pos1=3
pos2=11
str1ABC126HIJKDE

6、文字列文字列の比較
比較関数はstrcmpに似ています。sがパラメータで指定された文字列と等しいか、大きいか小さいかに応じて、s.compareは0、正または負を返します。

string str1 = "ABCDE";
	string str2 = "ABCDE";
	if (str1.compare(str2) == 0)
	{
    
    
		cout << "str1==strr2" << endl;
	}
	else if (str1.compare(str2) > 0)
	{
    
    
		cout << "str1>str2" << endl;
	}
	else
	{
    
    
		cout << "str1<str2" << endl;
	}

出力:

str1==strr2

7.文字列部分文字列
s.substr(pos、n)は、posから始まるsのn文字のコピーを含む文字列を返します。posのデフォルト値は0です。nのデフォルト値はs.size()-posです。これは、posで始まるすべての文字がコピーされることを意味します。

string str = "ABCDE";
	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 << endl;

出力

subStr=BCD
782347428

8.文字列
1を挿入および削除します。s.insert(pos、args)、argsで指定された文字をposの前に挿入します。posは、添え字またはイテレータにすることができます。下付き文字を受け取るバージョンはsへの参照を返し、イテレータを受け取るバージョンは最初に挿入された文字へのイテレータを返します。
2. s.erase(pos、len)、位置posから始まるlen文字を削除します。lenを省略すると、posからsの終わりまでのすべての文字が削除されます。sへの参照を返します。

    string str = "hello";
	str.insert(1, "111");//插入字符串
	cout << "str=" << str << endl;
	str.erase(1, 3);//删除从Pos开始的n个字符
	cout << "str=" << str << endl;

出力:

str=hlllello
str=hello

おすすめ

転載: blog.csdn.net/Little_XWB/article/details/108018758