C++硬货——string头文件【保姆级教学】

267 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第14天,[点击查看活动详情]

string头文件:

string头文件可以说是大家非常常用的一个头文件了,string头文件主要包括了(构造操作)、(赋值操作)、(字符串拼接)、(字符串的查找和替换)、(字符串的比较)、(字符串取字符)、(字符串的插入和删除)、(字符串子串)这几个操作,我将会一一介绍~

PS: 每种我都会给出代码,以及输出样例,在代码里有相应的文字讲解~

1.字符串的构造:

1.无参构造
2.const char*的拷贝构造
3.string的拷贝构造
4.n个elem构造

代码:

void test01() {
	//1.无参构造
	string s;
	cin >> s;
	cout << s << endl;
	//2.const char*拷贝构造
	const char* a = "abcd";
	string s2(a);
	cout << s2 << endl;
	//3.string拷贝构造
	string s3(s);
	cout << s3 << endl;
	//4.n个elem构造
	string s4(10, '*');
	cout << s4 << endl;
}

结果演示:

image.png

2.赋值:

1.“=”赋值,相应的参数有——(const char*)、(string)、(char)
2.“assign”赋值,相应的参数有——(const char*)、(const char*,n)、(const string&)、(int,char)

代码:

void test02() {
	const char* b = "Hello World!";
	//1.=赋值,(const char*)
	string a = b;
	cout << "a=" << a << endl;
	//(string)
	string c = a;
	cout << "c=" << c << endl;
	//(char)
	char x = '+';
	c = x;
	cout << "c=" << c << endl;
	
	//2.assign赋值
	string s;
	//(const char*)
	s.assign(b);
	cout << "s=" << s << endl;
	//(const char*,n)
	string s1;
	s1.assign(b, 3);
	cout << s1 << endl;
	//(const string&)
	string s2;
	s2.assign(s1);
	cout << s2 << endl;
	//(int,char)
	string s3;
	s3.assign(10, '+');
	cout << s3 << endl;
}

结果演示:

image.png

3.字符串拼接:

1.+=拼接,参数——(char)、(const char*)、(string)
2.append拼接,参数——(const char*)、(const char*,int)、(string)、(string,int pos,int n)

代码:

//字符串拼接
void test03() {
	const char* a = "Hello";
	string s = "abcd";
	//1.+=拼接
	//(const char*)
	string b;
	b += a;
	cout << "b=" << b << endl;
	//(char)
	b += '+';
	cout << "b=" << b << endl;
	//(string)
	b += s;
	cout << "b=" << b << endl;

	//2.append拼接
	//(const char*)
	string s1;
	s1.append(a);
	cout << "s1=" << s1 << endl;
	//(const char*,int n)
	s1.append(a, 3);
	cout << "s1=" << s1 << endl;
	//(string)
	s1.append(s);
	cout << "s1=" << s1 << endl;
	//(string,int pos,int n)
	s1.append(s, 2, 3);
	cout << "s1=" << s1 << endl;
}

结果演示:

image.png

4.字符串的查找和替换:

代码:

void test04() {
	string s1 = "abcdefgde";
	//查找
	int pos1 = s1.find("de");
	int pos2 = s1.rfind("de");
	cout << "pos1=" << pos1 << endl;
	cout << "pos2=" << pos2 << endl;

	//替换
	string s2 = "abcdefg123456";
	s2.replace(1, 3, "111111111");
	cout << "s2=" << s2 << endl;
}

演示结果:

image.png

5.字符串比较:

代码:

void test05() {
	//一一比较,因为H>A,所以返回"s1<s2"
	string s1 = "Apple";
	string s2 = "Hello";
	int x = s1.compare(s2);
	if (x == 0)
		cout << "s1=s2" << endl;
	else if (x > 0)
		cout << "s1>s2" << endl;
	else
		cout << "s1<s2" << endl;
}

演示结果:

image.png

6.字符存取:

1.用数组形式【】
2.用成员函数at

代码:

void test06() {
	string s = "abcdefgh";
	//1.[]
	for (int i = 0; i < s.size(); i++)
		cout << s[i] << " ";
	cout << endl;
	//2.at
	for (int i = 0; i < s.size(); i++)
		cout << s.at(i) << " ";
	cout << endl;
}

演示结果:

image.png

7.插入和删除:

1.insert函数插入
2.erase函数删除

代码:

void test07() {
	//1.(int pos,const char*s)
	string s = "abcdefgh";
	s.insert(3,"123");
	cout << s << endl;
	//(int pos,string)
	string s2 = "hhh";
	s.insert(7, s2);
	cout << s << endl;
	//2.const char*
	const char* a = "XYG";
	s.insert(11, a);
	cout << s << endl;
	//3.n个c
	s.insert(16, 4, '*');
	cout << s << endl;

	//删除
	//删除3开始的10个字符
	s.erase(3, 10);
	cout << s << endl;
}

演示结果:

image.png

8.string子串:

substr成员函数(int pos,int n),从pos开始的n个字符

代码:

void test08() {
	string s = "abcdefgh";
	//sbstr是s从1开始的5个字符组成的子串
	string sbstr = s.substr(1, 5);
	cout << "s=" << s << endl;
	cout << "sbstr=" << sbstr << endl;
}

演示结果:

image.png PS: 以上就是string头文件的大部分操作了,希望大家能够加深对string字符串的理解