C++(12)——string

本文详细解释了C++标准库中string类的四个主要成员函数:insert用于插入元素,erase用于删除元素,find用于查找子串,replace用于替换子串。通过实例展示了这些函数的用法和常见应用场景。
摘要由CSDN通过智能技术生成

目录

1.insert: 

1.1  string& insert (size_t pos, const string& str):

1.2  string& insert (size_t pos, const char* s):

1.3  string& insert (size_t pos, const char* s, size_t n):

1.4  string& insert (size_t pos, size_t n, char c):

1.5  iterator insert (iterator p, char c):

2. erase:

2.1 string& erase (size_t pos = 0, size_t len = npos):

2.2 iterator erase (iterator p):

2.3 iterator erase (iterator first, iterator last):

3. find:

3.1 size_t find (const string& str, size_t pos = 0) const:

3.2 size_t find (const char* s, size_t pos = 0) const:

3.3 size_t find (const char* s, size_t pos, size_t n) const:

3.4 size_t find (char c, size_t pos = 0) const:

4. replace:

4.1 string& replace (size_t pos,  size_t len,  const string& str):


上篇文章中,介绍了string中一些函数的应用。本篇文章将介绍其他string中相关函数的应用。

1.insert: 

	
 string& insert (size_t pos, const string& str);

 string& insert (size_t pos, const char* s);

 string& insert (size_t pos, const char* s, size_t n);

 string& insert (size_t pos, size_t n, char c);

 iterator insert (iterator p, char c);

1.1  string& insert (size_t pos, const string& str):

对于本部分标题中的函数,其作用为在已有string类型的对象的第pos个位置插入另一个string类型的对象,具体方法如下:

int main()
{
	string s1("hello world");
	string s2("xxx");

	s1.insert(5, s2);
	cout << "插入后" << endl;
	cout << s1 << endl;
	
	return 0;
}

运行结果如下:

1.2  string& insert (size_t pos, const char* s):

对于本部分标题中的函数,其作用为在已有string类型的对象的第pos个位置插入一个常量字符串,具体使用方法如下:

string s1("hello world");

char a[] = { "aaaaaa" };
s1.insert(5, a);
cout << "插入后" << endl;
cout << s1 << endl;

运行结果如下:

1.3  string& insert (size_t pos, const char* s, size_t n):

对于本部分标题中的函数,其作用为在已有string类型的对象的第pos个位置插入另一个另一个常量字符串的前n个字符,具体使用方法如下:

string s1("hello world");
char a[] = { "aaaaaa" };
	
s1.insert(5, a, 1);
cout << "插入后" << endl;
cout << s1 << endl;

运行结果如下:

1.4  string& insert (size_t pos, size_t n, char c):

对于本部分标题中的函数,其作用为在已有string类型的对象的第pos个位置插入n个连续的字符。例如在一个string类型的对象的第5个位置插入两个连续的字符&:

string s1("hello world");

char a = '&';
	
s1.insert(5, 2,a);
cout << "插入后" << endl;
cout << s1 << endl;

运行结果如下:

此外,本函数经常用做在string类型对象中进行头插,例如:

string s1("hello world");
	
char a = '&';	
s1.insert(0,1,a);
cout << "插入后" << endl;
cout << s1 << endl;

运行结果如下:

1.5  iterator insert (iterator p, char c):

对于本部分标题中的函数,其作用为通过迭代器来向string类型的对象中插入字符,例如:

	string::iterator it = s1.begin();
	s1.insert(it, 'x');
	cout << "插入后" << endl;
	cout << s1 << endl;

	s1.insert(s1.begin(), 'c');
	cout << "插入后" << endl;
	cout << s1 << endl;

	s1.insert(it + 3, 's');
	cout << "插入后" << endl;
	cout << s1 << endl;
	

运行结果如下:

2. erase:

	
string& erase (size_t pos = 0, size_t len = npos);

iterator erase (iterator p);

iterator erase (iterator first, iterator last);

2.1 string& erase (size_t pos = 0, size_t len = npos):
 

对于函数erase,其功能是对已有的string类型的对象中的内容进行删除。对于本标题中的函数,其中的两个参数均有缺省值,对于第一个参数,即:

size_t pos = 0

意义为:如果不认为给定参数,则默认从对象的开头进行删除。

对于第二个参数,即:

size_t len = npos

其意义为:删除对象中内容的长度。如果不人为给定参数,则默认删除到npos,对于npos,是一个常数,大小为4294967295,在本部分可以理解为:如果没有给定参数,则直接删除对象中的所有内容。

对于不给参数调用erase,即:

int main()
{
	string s1("hello world");
	s1.erase();
	cout << s1 << endl;

	return 0;
}
运行结果为:

假如指定从对象的第5个字符,即下标为4开始删除一直到最后,即:

int main()
{
	/*void Test1();
	*/
	string s1("hello world");
	s1.erase(4);
	cout << s1 << endl;

	return 0;
}

运行结果为:


 假设指定从对象的第5个字符开始,删除到对象的第8个字符,即删除内容的长度为4

2.2 iterator erase (iterator p):

本部分标题给出的函数主要是让erase与迭代器进行结合,函数的参数只有一个,本函数的意义为:删除迭代器位置的字符。例如:

	string s1("hello world");
	s1.erase(s1.begin());
	cout << s1 << endl;

运行结果如下:

如果想删除对象中的第三个字符,也就是下标为2的位置的字符,即:


	string s1("hello world");
	s1.erase(s1.begin()+2);
	cout << s1 << endl;

 运行结果如下:

2.3 iterator erase (iterator first, iterator last):

对于本部分标题中的函数的参数与上一个函数相同,但是本函数是删除[first,last)这个区间的字符,例如:

string s1("hello world");

s1.erase(s1.begin()+2,s1.end()-2);
cout << s1 << endl;

运行结果如下:

3. find:

size_t find (const string& str, size_t pos = 0) const;
	
size_t find (const char* s, size_t pos = 0) const;
	
size_t find (const char* s, size_t pos, size_t n) const;

size_t find (char c, size_t pos = 0) const;

3.1 size_t find (const string& str, size_t pos = 0) const:

       对于本部分标题中的函数,其大致意义为:给定一个string类型的对象str,在已有的string类型的对象中去查找str,如果找到了,返回str在已有string类型的对象的下标,如果找不到则返回npos

      对于函数的第2个参数,如果不给定具体参数,则按照缺省参数从已有对象的值,从pos位置开始查找。具体使用如下:

int main()
{
	string s1("hello world");
	string s2("hello");
	string s3("world");

	size_t pos1 = s1.find(s2);
	size_t pos2 = s1.find(s3);
	cout << pos1 << endl;
	cout << pos2 << endl;

	return 0;
}

运行结果如下:

对于两个参数的使用,例如从下标为4的位置开始进行查找,即:

string s1("hello world");
string s3("world");
size_t pos3 = s1.find(s3, 4);
cout << pos3 << endl;

运行结果如下:

当不能查找到相应的内容时,例如:

	string s1("hello world");
	string s2("hello");
	
	size_t pos3 = s1.find(s2,6);
	cout << pos3 << endl;

 运行结果如下:

3.2 size_t find (const char* s, size_t pos = 0) const:

具体使用方法与3.1中相同,只是参数有改变,本部分只给出一个应用,其他不再进行叙述:
 


	char a1[] = "hello";
	size_t pos5 = s1.find(a1, 0);
	cout << pos5 << endl;

运行结果如下:

 

3.3 size_t find (const char* s, size_t pos, size_t n) const:

本标题中函数的大致意思为:在已有的string类型的第pos位置,查找字符串s的前n个字符,例如:

char a2[] = "helloyou";

	size_t pos6 = s1.find(a2, 0, 5);
	cout << pos6 << endl;

运行结果如下:

对于上面的字符串a2,如果是在已有的string类型的对象中寻找前6个字符,即在hello\,\, world中寻找helloy,即:

size_t pos6 = s1.find(a2, 0, 6);
	cout << "pos6" << ' ';
	cout << pos6 << endl;

运行结果为:

由于找不到匹配的内容,因此返回npos。 

3.4 size_t find (char c, size_t pos = 0) const:

本部分标题中函数的大致意义为:从已有的string类型的pos位置开始寻找一个字符c,如果不人为给定pos的值,则默认从开头进行查找,具体使用方法如下:

string s6("abcdefg");
	size_t pos7 = s6.find('a');
	size_t pos8 = s6.find('a', 1);
	cout << "pos7:" << pos7 << endl << "pos8:" << pos8 << endl;

运行结果如下:

4. replace:

	
string& replace (size_t pos,  size_t len,  const string& str);
string& replace (iterator i1, iterator i2, const string& str);
	
string& replace (size_t pos,  size_t len,  const string& str,
                 size_t subpos, size_t sublen);
	
string& replace (size_t pos,  size_t len,  const char* s);
string& replace (iterator i1, iterator i2, const char* s);

string& replace (size_t pos,  size_t len,  const char* s, size_t n);
string& replace (iterator i1, iterator i2, const char* s, size_t n);
	

由于replace的设计中,相似的部分过多,因此,文章只给出其中一个函数的使用

4.1 string& replace (size_t pos,  size_t len,  const string& str):

本部分标题函数的大致意义为:从已有string对象的pos位置开始,把相隔len个字符位置之内的内容,也就是[pos,len)替换成str

具体使用方法如下:

string s1("hello world");
string s2("xx");

s1.replace(0, 2, s2);
cout << s1 << endl;

运行结果如下:

对于不同的场景,可以结合上方的函数来解决不同的问题,例如:

"hello\, \, world\, \, hello\, \, everyone"中的空格全部替换为*

string s2("hello world hello everyone");
	size_t pos = s2.find(' ');
	while (pos != s2.npos)
	{
		s2.replace(pos,1, "*");
		pos = s2.find(' ');
	}
	cout << s2 << endl;

运行结果如下:

但是这种方法每次循环都要从头去找空格所在的位置,并且replace函数本身的作用原理就类似顺序表中的头插,效率过低,因此采用下面的方法:

string s4("hello world hello everyone");
	string s3;
	for (auto ch: s4)
	{
		if (ch == ' ')
		{
			s3 += '*';
		}
		else
		{
			s3 += ch;
		}
	}
	cout << s3 << endl;

运行结果如下:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

起床写代码啦!

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值