string类常见接口和一些习题

本文详细介绍了C++中string类的基本概念和常用操作,包括构造函数、析构函数、赋值重载、迭代器的使用、容量相关函数、字符访问以及插入删除操作。通过实例代码展示了如何创建、遍历和修改string对象,并探讨了string对象的内存管理和容量调整。同时,文章还包含了若干练习题以加深理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

string类的简单概念理解

1,首先,标准库给我们提供了提供string类,使用的时候需要使用标准命名空间
using namespace std; 另外,它帮我们封装好了成员函数,和成员变量
这样外面在解决问题的时候,我们可以直接调用早就写好的成员函数了。

    class string
{
	//构造函数
	//析构函数
	//........各种功能的函数  ,大佬已经帮我们写好了


private:
	char* _str;
	size_t capacity;
	size_t size;
};

C++文档网站

我们需要去网站里面去查看文档去学习
这个比较好,不混乱 ,早期用这个,推荐这个 : https://cplusplus.com/
这个是官方网站,比较混乱,但是更新的比较快,适合查看新语法:https://en.cppreference.com/w/

点开之后直接搜索string即可

先看看构造函数,析构函数,赋值重载

析构函数没什么,就是释放空间,对象的生命周期结束后自动调用。
在这里插入图片描述

#include <iostream>
using namespace std;

int main()
{
	string s1();          //无参构造函数,这是个匿名对象,生命周期只在这一行
	string s2("hello world");   //传字符串的构造函数
	string s3(s2);            //传同类型的构造函数
	cout << s3 << endl;
	
	string s4("haha");        
	s4 = s3;                   //同类型的赋值重载
	cout << s4 << endl;

	s4 = "24556";            //const char* 的赋值重载
	cout << s4 << endl;

	s4 = 'a';                //单个字符的赋值重载
	cout << s4 << endl;
	return 0;
}

在这里插入图片描述

看看迭代器iterator,如何去遍历

不用想的太复杂,就是类似于指针的东西,我们现在会用就可以了

int main()
{
	string s1("hello world");
	string::iterator it = s1.begin();   //这里的作用域操作符,指定的是string的迭代器
	while (it != s1.end())          
	{
		cout << *it << " ";
		it++;
	}

	string::reverse_iterator rit = s1.rbegin();   //倒着遍历
	while (rit != s1.rend())
	{
		cout << *rit << " ";
		it++;
	}
	return 0;
}

在这里插入图片描述

capacity 关于容量的函数

在这里插入图片描述

int main()
{
	string s1("hello world");
	cout << s1.size() << endl;    //输出的有效字符的个数,\0是标识字符

	cout << s1.capacity() << endl;  //返回的是空间大小,编译器自己存在扩容机制

	//关于resize 和 reserve (重要)

	s1.resize(15);      //这里会自动填充‘\0’
	s1.reserve(10);     //什么事情都不做
	s1.reserve(50);   

	cout << s1.capacity() << endl;  //输出不是50,
	return 0;
}

在这里插入图片描述

如何访问字符呢?

在这里插入图片描述
在这里插入图片描述

各种插入删除操作

在这里插入图片描述


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

	s1 += s2;    //这里的作用是把s2追加到s1
	cout << s1 << endl;
	 
	s1 += "#######";   //这里是追加字符串
	cout << s1 << endl;


	string s3("i am a handsome boy");
	string s4("you are a good girl");

	//s3.append(s4);      //类似于+=  ,s4追加到s3 的后面
	//cout << s3 << endl;

	//s3.append(s4,5);   //从第5个位置开始追加
	//cout << s3 << endl;

	s3.append(s4, 5,2);   //从第5个位置开始追加,只追加2个
	cout << s3 << endl;

	//s3.append("!!!!!!!!!!!!");  //追加字符串
	//cout << s3 << endl;

	s3.append("!!!!!!!!!!!!",3);  //追加3个字符串
	cout << s3 << endl;
	return 0;
}

一些零零散散比较重要的

在这里插入图片描述

string的习题

int main()

{

string a="hello world";

string b=a;

if (a.c_str()==b.c_str()) 

{

cout<<"true"<<endl;

}

else cout<<"false"<<endl;

string c=b;

c="";

if (a.c_str()==b.c_str())

{

cout<<"true"<<endl;

}

else cout<<"false"<<endl;

a="";

if (a.c_str()==b.c_str())

{

cout<<"true"<<endl;

}

else cout<<"false"<<endl;

return 0;

}

解析:string的拷贝构造和赋值重载都是深拷贝,c_str()返回的是指向的空间,那肯定都是不相等的

求下面的输出结果
int main()

{

string str(“Hello Bit.”);

str.reserve(111);

str.resize(5);

str.reserve(50);

cout<<str.size()<<“:”<<str.capacity()<<endl;

return 0;

}
解析:resever调整空间大小,比原空间小,不做任何事情, 所以 5,111

看下面结果的输出,

int main()

{

string strText = "How are you?";

string strSeparator = " ";

string strResult;

int size_pos = 0;

int size_prev_pos = 0;

while((size_pos=strText.find_first_of(strSeparator, size_pos)) != string::npos)  //从size_pos位置查找空格’ ‘字符返回下标

{

strResult = strText.substr(size_prev_pos, size_pos-size_prev_pos);    //然后拿出strText从prepos的位置向后n个的字符

cout<<strResult<<" ";

size_prev_pos = ++size_pos;

}

if(size_prev_pos != strText.size())

{

strResult = strText.substr(size_prev_pos, size_pos-size_prev_pos);

cout<<strResult<<" ";

}

cout<<endl;

return 0;

}

解析:请看注释,结果是 How are you?

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

通过全部用例

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

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

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

打赏作者

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

抵扣说明:

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

余额充值