探索C++的string:从基础到深入

发布于:2024-05-12 ⋅ 阅读:(150) ⋅ 点赞:(0)

在这里插入图片描述

string类

C++中的string类是一个非常重要的字符串处理工具,它提供了一种方便且灵活的方式来处理字符串。它位于标准命名空间std下,因此通常需要使用using namespace std;语句或者前缀std::来引用。
在string类中有很多接口,学习这些接口对后面学vector、list还有stack和Queue有帮助。

string类的接口

string的常见构造

函数名称 功能说明
string() 构造空的字符串
string(const char* s) 用一个C-string来构造一个string类
string(size_t n, char c) string类包含n个c字符
string(const string&s) 拷贝构造函数

举例:

int main()
{
	string s1();
	string s2("hello world");
	string s3(10, 'c');
	string s4(s2);
}

在这里插入图片描述

string类对象的容量操作

函数名称 函数功能
size 返回字符串的有效长度
length 返回字符串的有效长度
capacity 返回空间大小
empty 判断字符串是否为空
clear 清空有效字符串
reserve 为字符串预留空间
resize 将有效字符串改成n个,多出的长度用字符c填充

1. size函数

void size()
{
	string s1("hello world");
	int size = s1.size();//11
	cout << size << endl;
}

注意:size函数和strlen一样都没有把\0算进去

2. length函数
length和size一样,都没有将最后末尾的\0算在长度当中

3. capacity函数

对比size函数:

void test_string()
{
	string s1("hello world");
	cout << s1.capacity() << endl;
	cout << s1.size() << endl;
}

capacity是返回总的大小,而size是返回的是有效字符串的长度。
在这里插入图片描述

4.empty函数

void test_string2()
{
	string s1;
	string s2("hello world");
	cout << s1.empty() << endl;//1
	cout << s2.empty() << endl;//0
}

5.clear函数

void test_string3()
{
	string s1("hello world");
	cout << s1 << endl;
	s1.clear();
	cout << s1 << endl;
}

在这里插入图片描述
可以看见,打印出来的是空字符串。

6.reserve函数

void test_string4()
{
	string s1("hello world");
	cout << s1 << endl;
	s1.reserve(100);
	s1 += " C++";
	cout << s1 << endl;
}

当我们预留了空间之后,就不需要担心空间不够时额外的开销了,reserve的使用场景,常用在当我们知道我们接下来的对空间的开销时,我们可以直接预开辟一段空间,因为有些编译器增加空间时不是常规增加的,所以如果我们不断的开辟空间会有很多空间的浪费,所以我们可以直接预开辟一段空间,这样就减少了空间的浪费。

7.resize函数

void test_string5()
{
	string s1("hello world");
	cout << s1 << endl;
	s1.resize(15, 'c');
	cout << s1 << endl;
}

在这里插入图片描述

string类的遍历及访问操作

函数名 函数功能
operator[] 返回pos位置的字符
begin+end begin获取一个迭代器,end获取一个迭代,用迭代器进行遍历访问
ebegin+rend 和begin和end相仿,一个是从前往后,一个是从后我往前访问
范围for 范围for的底层也是迭代器

1.operator[]

void test_string6()
{
	string s1("hello world");
	for (int i = 0;i < s1.size();i++)
	{
		cout << s1[i] << ' ';
	}
}

2.begin+end

void test_string7()
{
	string s1("hello world");
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it << ' ';
		it++;
	}
}

迭代器可以理解为指针,只是理解(但绝对不是指针),我们可以看看

用下面代码输出it的类型

void test_string8()
{
	string s1("hello world");
	string::iterator it = s1.begin();
	cout << typeid(it).name() << endl;
}

class std::_String_iterator<class std::_String_val<struct std::_Simple_types<char> > >,可以看见it的类型并不是指针,而是一个类,底层可以对*进行了重载,然后和指针的用法相似而已。

3.rbegin+rend

void test_string9()
{
	string s1("hello world");
	string::reverse_iterator it = s1.rbegin();
	while (it != s1.rend())
	{
		cout << *it << ' ';
		it++;
	}
}

注意:rbegin和rend需要用reserve_iterator这个迭代器,而不是iterator。

输出效果:d l r o w o l l e h

4.范围for

void test_string10()
{
	string s1("hello world");
	for (auto e : s1)
	{
		cout << e << ' ';
	}
}

string类对象的修改操作

函数名称 函数功能
push_back 在字符串中尾插一个字符
append 在字符串后追加一个字符串
operator+= 在字符串后追加一个字符串str
c_str 返回c格式的字符串
find+npos 从字符串的pos位置往后找c字符,找到了就返回该字符的位置
rfind 从pos位置往前找c字符,找到了就返回其位置
substr 在str中从pos位置开始截取n个字符,返回子串

1.push_back

void test_string11()
{
	string s1("hello world");
	cout << s1 << endl;
	s1.push_back('c');
	cout << s1 << endl;
}

hello world
hello worldc

2.append

void test_string12()
{
	string s1("hello world");
	cout << s1 << endl;
	s1.append("hello");
	cout << s1 << endl;
}

输出:
hello world
hello worldhello

3.operator+=

operator+=和append类似,但是比append好用

void test_string13()
{
	string s1("hello world");
	cout << s1 << endl;
	s1 += "hello";
	cout << s1 << endl;
}

4.c_str

void test_string14()
{
	string s1("hello world");
	cout << s1 << endl;
	const char* s = s1.c_str();
	cout << s << endl;
}

5.find+npos
下面是一段网址,我们可以对网址进行分段,就可以用到find

void test_string15()
{
	string s1("https://www.youtube.com/watch?v=mkRNzJ5iasA");
	size_t pos = s1.find(':');
	string s2 = s1.substr(0, pos);
	cout << s2 << endl;
	size_t pos1 = s1.find('/', pos + 3);
	string s3 = s1.substr(pos + 3, pos1 - (pos + 3));
	cout << s3 << endl;
}

输出:
https
www.youtube.com

6.rfind
rfind可以参考find。

7.substr

void test_string16()
{
	string s1("hello world");
	string s2 = s1.substr(0, 5);
	cout << s2 << endl;
}

string类的非成员函数

函数名 函数功能
operator+ 传值返回浅拷贝
operator>> 运算符重载
operator<< 运算符重载
getline 获取一行字符,包含空格

1.operator+

void test_string17()
{
	string s1("hello ");
	string s2("world");
	cout << (s1 + s2) << endl;
	cout << (s1 + "world") << endl;
	cout << ("world" + s1) << endl;
}

输出:
hello world
hello world
worldhello

operator>>和operator<<就很简单,这里略过
2.getline

void test_string18()
{
	string s1;
	getline(cin, s1);
	cout << s1 << endl;
}

这里注意getline的参数就可以了,第一个参数是istream第二个参数是需要输入的字符串

总结

在本博客中,我们深入探讨了C++中的string类,这是标准库中用于处理字符串的重要工具。我们首先介绍了string类的基本概念和使用方法,包括创建、初始化、访问字符、以及与其他string对象的比较等。接着,我们探讨了string类的各种成员函数,例如append、substr、find等,这些函数提供了丰富的字符串操作功能,使得处理字符串变得更加方便和高效。此外,我们还讨论了string类与C风格字符串之间的区别,以及在实际开发中应该如何选择使用。

总的来说,string类为C++程序员提供了一个强大且易于使用的字符串处理工具,它不仅提供了丰富的功能和灵活性,还避免了C风格字符串可能导致的一些问题,如内存管理和越界访问等。通过深入理解和熟练掌握string类,我们可以更轻松地处理各种字符串操作,并编写出更加健壮和可维护的代码。
在今天的学习中,我们不仅学会了如何正确使用string类,还了解了它的内部实现原理和一些性能优化技巧,这些知识对于提高我们的编程技能和解决实际问题都具有重要意义。希望通过本文的分享,读者们对string类有了更深入的理解,并能够在自己的项目中充分发挥它的作用。愿大家在未来的编程之路上越走越远,不断提升自己,创造出更加优秀的作品!

感谢大家的阅读与支持!


网站公告

今日签到

点亮在社区的每一天
去签到