[C ++コンテナ]文字列

1.文字列の基本概念

自然:

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

stringとcharの違い*:

  • char *はポインタです

  • Stringは、char *をカプセル化し、char *タイプのコンテナーであるこの文字列を管理するクラスです。

特徴:

多くのメンバーメソッドは文字列クラス内にカプセル化されています

例:検索検索、コピーコピー、削除削除置換置換、挿入挿入

Stringは、char *によって割り当てられたメモリを管理します。範囲外のコピーや範囲外の値などについて心配する必要はありません。これは、クラスの責任です。

2.文字列コンストラクタ

コンストラクターのプロトタイプ:

  • string(); //空の文字列を作成します例:string str;

  • string(const char* s); //文字列sを使用して初期化します

  • string(const string& str); //文字列オブジェクトを使用して別の文字列オブジェクトを初期化します

  • string(int n, char c); // n文字cを使用して初期化します

#include <iostream>
#include <string>
using namespace std;

//string的构造函数

/*
string();         				//创建一个空的字符串 例如: string str;
string(const char* s);          //使用字符串s初始化
string(const string& str);      //使用一个string对象初始化另一个string对象
string(int n, char c);          //使用n个字符c初始化


*/

void test01()
{
	string s1;     //默认构造函数

	const char* str = "hello world";
	string s2(str);
	cout << s2 << endl;

	//第三种
	string s3(s2);
	cout << s3 << endl;

	//第四种
	string s4(4, 'a');
	cout << s4 << endl;
}

int main(void)
{
	test01();
	system("pause");
	return 0;
}

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を使用して、現在の文字列を割り当てます

#include <iostream>
#include <string>
using namespace std;


/*
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赋给当前字符串


*/

void test02()
{
	//第一种
	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(10, 'w');
	cout << "str7 = " << str7 << endl;
}

int main(void)
{
	test02();
	system("pause");
	return 0;
}

総括する:

文字列値operator=割り当てる方法はたくさんありますが、この方法の方が実用的です

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文字は、文字列の末尾に接続されます

#include <iostream>
#include <string>
using namespace std;

/*
string字符型拼接

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);                    //同operator+=(const string& str)
string& append(const string &s, int pos, int n);    //字符串s中从pos开始的n个字符连接到字符串结尾

*/


void test03()
{
	//第一种
	string str1 = "我";
	str1 += "爱玩游戏";
	cout << "str1 = " << str1 << endl;

	//第二种
	str1 += 'a';
	cout << "str1 = " << str1 << endl;

	//第三种
	string str2 = "LOL DNF";
	str1 += str2;
	cout << "str1 = " << str1 << endl;

	//成员函数第一种
	string str3 = "I";
	str3.append("love");
	cout << "str3 = " << str3 << endl;

	//成员函数第二种
	str3.append("game abcde", 4);
	cout << "str3 = " << str3 << endl;

	//成员函数第三种
	string str4 = "sssss";
	str3.append(str4);
	cout << "str3 = " << str3 << endl;

	//成员函数第四种
	str3.append(str2, 0, 3);
	cout << "str3 = " << str3 << endl;
}

int main(void)
{
	test03();
	system("pause");
	return 0;
}

5.文字列検索と置換

機能の説明:

  • 検索:指定された文字列が存在するかどうかを検索します

  • 置換:指定された位置で文字列を置換します

関数プロトタイプ:

検索

  • int find(const string& str, int pos = 0) const; // posから始めて、strの最初の出現位置を見つけます

  • int find(const char* s, int pos = 0) const; // sの最初の出現位置を、posから開始して検索します

  • 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に置き換えます

#include <iostream>
#include <string>
using namespace std;

/*
string的查找和替换

查找
int find(const string& str, int pos = 0) const;              //查找str第一次出现位置,从pos开始查找【沒有返回-1】
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最后一次出现位置

替换
string& replace(int pos, int n, const string& str);           //替换从pos开始n个字符为字符串str
string& replace(int pos, int n,const char* s);                //替换从pos开始的n个字符为字符串s

*/

//查找
void test04()
{
	string str1 = "abcdefg";
	int pos = str1.find("de");
	if (pos == -1)
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << "首次出现的位置下标pos = " << pos << endl;
	}
	

	//rfind 和find的区别
	//rfind是从右往左查找,find是从左往右查找
	pos = str1.rfind("de");
	cout << "pos = " << pos << endl;
}

//替换
void test004()
{
	string str1 = "abcdefg";
	//从1号位置起 3个字符 替换成 "1111"
	str1.replace(1, 3, "1111");    //替换1111
	cout << "str1 = " << str1 << endl;
}


int main(void)
{
	test04();
	test004();
	system("pause");
	return 0;
}

総括する:

  • 検索は左から後ろへ、rfindは右から左へ

  • 検索は、文字列を検索した後の最初の文字位置を返し、見つからない場合は-1を返します。

  • 置き換えるときは、どの位置から、何文字、どの種類の文字列を置き換えるかを指定する必要があります

6、文字列文字列の比較

機能の説明:

  • 文字列間の比較

比較方法:

  • 文字列の比較は、文字のASCIIコードを比較することです

= 0を返す

> 1を返す

<戻り値-1

関数プロトタイプ:

  • int compare(const string &s) const; //文字列と比較します

  • int compare(const char *s) const; //文字列と比較します

#include <iostream>
#include <string>
using namespace std;

//string字符串比较

/*
int compare(const string &s) const;   //与字符串s比较
int compare(const char *s) const;     //与字符串s比较

*/
void test05()
{
	string str1 = "xello";
	string str2 = "hello";
	if (str1.compare(str2)==0)
	{
		cout << "str1 等于 str2" << endl;
	}
	else if(str1.compare(str2)>0)
	{
		cout << "str1 大于 str2" << endl;
	}
	else
	{
		cout << "str1 小于 str2" << endl;
	}
}

int main(void)
{
	test05();
	system("pause");
	return 0;
}

7、文字列アクセス

文字列内の1文字にアクセスする方法は2つあります

  • char& operator[](int n); // []を介して文字をフェッチします

  • char& at(int n); // atメソッドを介して文字を取得します

#include <iostream>
#include <string>
using namespace std;

/*
char& operator[](int n);     //通过[]方式取字符
char& at(int n);             //通过at方法获取字符

*/

void test06()
{
	string str = "hello";
	cout << "str = " << str << endl;

	//对单个字符进行存储
	//1 通过[]访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout<<str[i] <<" ";
	}
	cout<<endl;
	//2 通过at方式访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout <<str.at(i) << " ";
	}
	cout << endl;

	//修改
	str[0] = 'A';
	cout << "str = " << str << endl;

	str.at(0) = 'h';
	cout << "str = " << str << endl;
}

int main(void)
{
	test06();
	system("pause");
	return 0;
}

概要:文字列内の1つの文字にアクセスするには、[]またはatを使用する2つの方法があります。

8、文字列の挿入と削除

機能の説明:

  • 文字列の文字を挿入および削除します

関数プロトタイプ:

  • 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); //位置から始まるn文字を削除します

#include <iostream>
#include <string>
using namespace std;


/*
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个字符

*/

void test07()
{
	string str = "hello";
	cout << "str = " << str << endl;

	//插入
	str.insert(1, "222");      //h222ello
	cout << "str = " << str << endl;

	//删除
	str.erase(1,3);
	cout << "str = " << str << endl;
}


int main(void)
{
	test07();
	system("pause");
	return 0;
}

概要:挿入と削除の開始インデックスは0から始まります

9、文字列部分文字列

機能の説明:

  • 文字列から目的の部分文字列を取得します

関数プロトタイプ:

  • string substr(int pos = 0, int n = npos) const; // posで始まるn文字で構成される文字列を返します

#include <iostream>
#include <string>
using namespace std;


//string substr(int pos = 0, int n = npos) const;    //返回由pos开始的n个字符组成的字符串

void test08()
{
	string str = "abcdef";
	string subStr = str.substr(1, 3);     //"bcd"
	cout << "subStr = " << subStr << endl;
}

//使用操作
void test008()
{
	string email = "[email protected]";
	//从邮箱地址中 获取用户名信息
	//查找@位置
	int pos = email.find("@");
	string userName = email.substr(0, pos);
	cout << "userName = " << userName << endl;
}
int main(void)
{
	test08();
	test008();
	system("pause");
	return 0;
}

概要:部分文字列関数を柔軟に使用すると、実際の開発で効果的な情報を取得できます

おすすめ

転載: blog.csdn.net/Zhouzi_heng/article/details/115077128