【C++第三阶段】string容器

以下内容仅为当前认识,可能有不足之处,欢迎讨论!



string容器

基本概念

本质👉string是C++风格的字符串,string本质上是一个类。

string VS char *

char *是一个指针。

string是类,里面封装char* , 类管理该字符串,是一个char*型的容器。

特点:

string类内部封装很多成员方法,例如查找find , 拷贝copy , 删除delete , 替换replace ,插入insert。

string管理char*分配内存,不用担心复制越界和取值越界等,由类内部负责。

构造函数

无参构造,字符串初始化,拷贝构造函数,以及n字符c初始化。

n字符c必须要是单引号。

以下是代码示例。

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

void test0403() {
	//先定义一个常量字符串
	const char* str = "hello world";

	//无参构造函数
	string no_param_str;

	//有参构造函数
	string param_str(str);

	//拷贝构造函数
	string copy_str(param_str);

	//n字符c构造函数
	string nc_str( 10,'?');

	cout << "无参构造:" << no_param_str << endl;
	cout << "有参构造:" << param_str << endl;
	cout << "拷贝构造:" << copy_str << endl;
	cout << "n字符c构造:" << nc_str << endl;


}

int main() {

	test0403();

	system("pause");
	return 0;
}

运行截图:

image-20240403164039020

赋值操作

提供7种赋值操作。方法为自己写的myAssign

const char *s 是双引号的字符出串,const string &s是单独的字符串变量名字。

  • char * 类型字符串,赋值给当前的字符串
  • string & operator =(const char *s);
  • 把字符串s赋值给当前的字符串
  • string & operator=(const string &s);
  • 字符赋值给当前的字符串
  • string & operator=(char c);
  • 把字符串赋给当前的字符串
  • string & assign(const char *s);
  • 把字符串s的前n个字符赋值给当前的字符串
  • string & assign(const char *s,int n );
  • 把字符串赋值给当前字符串
  • string & assign(const string &s);
  • 用n个字符c赋值给当前字符串
  • string & assign(int n , char c);
void myAssign() {
	string str_a = "hello"; //1.
	string str_b = str_a;  //2.

	//string& operator = (const string & str);
	string str_c;
	str_c = 'a';

	string str_d;
	str_d.assign(str_c);

	//char *b = 'abcdefg';
	string str_e;
	str_e.assign("hello,world", 11);

	string str_f;
	str_f.assign(str_e);

	string str_g;
	str_g.assign(str_e,0 ,5); //从0到4,5个字符

	string str_h;
	str_h.assign(5, 'w');

	cout << "str a = " << str_a <<" . " <<endl;
	cout << "str b = " << str_b <<" . " <<endl;
	cout << "str c = " << str_c <<" . " <<endl;
	cout << "str d = " << str_d <<" . " <<endl;
	cout << "str e = " << str_e <<" . " <<endl;
	cout << "str f = " << str_f <<" . " <<endl;
	cout << "str g = " << str_g <<" . " <<endl;
	cout << "str h = " << str_h << " . " << endl;



}

image-20240407203653931

拼接操作

提供7种拼接操作。

  • 直接拼接字符数组

  • string & operator+=(const char * str);

  • 拼接单个字符

  • string & operator+=(const char c);

  • 拼接字符串

  • string & operator +=(const string & str);

  • 追加至末尾单个字符

  • string & append(const char *s);

  • 追加字符串的前n个字符至当前字符末尾

  • string & append(const char *s , int n);

  • 追加字符串

  • string & append(const string &s);

  • 从pos位置开始,追加n个字符到当前字符串末尾

  • string & append(const string &s , int pos , int n);

void myAppend() {
	string str = "aaa";
	cout << "str = " << str << endl;

	
	str += 'b';
	cout << "str = " << str << endl;

	str += "c";
	cout << "str = " << str << endl;

	str.append("e"); //当我输入'e'时,说没有这个重载。
	cout << "str = " << str << endl;

	str.append("world,hello", 5);
	cout << "str = " << str << endl;

	str.append("hello");
	cout << "str = " << str << endl;

	str.append("microsoft visual", 0, 9);
	cout << "str = " << str << endl;
}

image-20240407203124496

字符串查找和替换

查找:查找指定字符串是否存在

  • 查找字符str第一次出现的位置,从pos的位置开始查找。
  • int find(const string & str , int pos = 0) const;
  • 查找字符串s第一次出现的位置,从pos位置开始找。
  • int find(const char* s ,int pos = 0) const;
  • 查找字符串s前n个字符中,从pos位置开始找。
  • int find(const char* s , int pos , int n) const;
  • 找字符c出现的位置,从pos位置开始找
  • int find(const char c ,int pos = 0) const;

rfind的索引仍然是从左往右来看的,但是查找时是从右往左。

  • 倒着查找,找字符串第一次出现的位置
  • int rfind(const string & str , int pos = npos) const;
  • 倒着查找,找字符串中
  • int rfind(const char*s , int pos ,int n) const;
  • 倒着查找,从pos查找s的前n个字符最后一次出现位置
  • int rfind(const char c , int pos ,int n) const;
  • 查找字符c最后一次出现位置。
  • int rfind(const char c , int pos = 0) const;

代码示例

void myFind() {
	string str = "hello,world,olleh,dlrow";
	string test = "le";
	int a, b, c, d, e, f, g, h;
	

	a = str.find(test, 0);
	cout << a << endl;

	b = str.find("el", 0);
	cout << b << endl;

	c = str.find("e", 0,10);
	cout << c << endl;

	d = str.find('e', 0);
	cout << d << endl;

	e = str.rfind(test, -1);//从row那里开始从右往左找,应该是14,
	cout << e << endl;

	f = str.rfind("l", -1);//从row那里开始从右往左找,应该是19
	cout << f << endl;

	g = str.rfind("le", -1,2);//从row开始从右往左找,从右往左第3个位置开始找,应该是14
	cout << g << endl;

	h = str.rfind('l', -1);//从row那里开始从右往左找,应该是19
	cout << h << endl;

}

image-20240407212149095

替换:在指定位置替换字符串

  • pos位置起始的n个字符替换成str
  • string & replace (int pos , int n , const string& str);
  • pos位置起始的n个字符替换成s
  • string & replace(int pos , int n , const char* s);
void myReplace() {
	string str = "hello,world;";
	string test = "?????";
	str.replace(0, 3, test);
	cout << str << endl;

	str.replace(0, 5, "a");
	cout << str << endl;

}

image-20240407212528184

字符串比较

比较按照字符串的ASCII码比较

  • 与字符串s比较

  • int compare(const string &s) const;

  • int compare (const char *s) const;

void myCompare() {
	string str = "hello";
	string rts = "hello";

	int result = str.compare(rts);
	if (result==0) {
		cout << "两者一样" << endl;
	}
	else {
		cout << "两者不一样" << endl;
	}
}

image-20240407214801444

字符串存取

两种方式

char& operator[] (int n );

char& at(int n);

void myInOut() {
	string str = "hello?world";
	cout << str[1] << endl;

	cout << str.at(2) << endl;
}

image-20240407214948544

字符串插入和删除

四种方式

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 myInsertDelete() {
	string str = "空空如也";
	str.insert(0, "?");
	cout << "str = " << str << endl;

	str.insert(3, "!!!") ;
	cout << "str = " << str << endl;

	str.insert(5, 5, '.');
	cout << "str = " <<str<< endl;

	str.erase(5, 2);
	cout << "str = " << str << endl;
}

这个位置说的是字符的具体位置,不是索引。

image-20240407215620139

字符串子串

从字符串中获取想要的子串。

string substr(int pos = 0 , int n = npos) const;

void mySubString() {
	string str = "hello";
	string sub = str.substr(0, 4);
	cout << "sub = " << sub << endl;
}

image-20240407220057016


以上是我的学习笔记,希望对你有所帮助!
如有不当之处欢迎指出!谢谢!

学吧,学无止境,太深了

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HelpFireCode

随缘惜缘不攀缘。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值