当前位置: 首页 > article >正文

【STL】string类的使用

🌟🌟作者主页:ephemerals__
🌟🌟所属专栏:C++、STL

目录

string类的介绍--为什么学习string类

一、string类的默认成员函数 

构造函数(constructor)

析构函数(destructor)

赋值运算符重载operator=

二、string类的容量接口

size和length

capacity

clear

empty

reserve

resize

三、string对象的访问及遍历操作

1. string类的元素访问接口

operator[ ]

at

front和back

2. 迭代器接口

begin和end

rbegin和rend

cbegin、cend、crbegin、crend

3. string类字符串的三种遍历方法

3.1 下标访问

3.2 迭代器访问

3.3 范围for

四、string类的修改操作接口

operator+=

append

push_back和pop_back

assign

insert

erase

replace

swap

五、string类的字符串运算相关接口

c_str

find和rfind 

substr

六、string类的常量成员

七、string类的非成员函数

relational operators(关系运算符重载)

operator>>和operator<<

getline

swap 

总结


string类的介绍--为什么学习string类

        之前我们对STL已经有了一些初步的了解,本篇文章我们正式开始学习STL。我们都知道,在C语言当中,有一些库函数:strlen、strcpy、strcmp、strstr......它们都是处理字符串的函数。但是这些函数的定义与字符串是分离的,并不符合面向对象编程的思想。c++标准库当中,定义了一个用于表示字符串及其操作,叫做stringstring类最开始并不属于STL,但是它在c++标准库中的作用与STL紧密相连,于是成为了STL的一员。与C语言的字符数组和头文件string.h相比,string类具有更丰富的功能、更高的安全性和更便捷的操作方式。本篇文章,我们一起学习探讨string类的一些常用接口及使用方法

        小贴士:在我们使用string类时,要引头文件<string>,并且该类定义在命名空间std当中。

        string类相关接口查阅:

string - C++ Reference (cplusplus.com)

一、string类的默认成员函数 

        string类显示实现的默认成员函数有三种:

构造函数(constructor)

在c++11中,string有九个构造函数,但最常用的构造函数有四个: 

函数原型功能说明
string();无参构造,创建一个空的string类对象,即空字符串
string(const string& str);拷贝构造,用一个string对象构造另一个对象
string(const char* s);用一个字符数组构造一个string类对象
string(size_t n, char c)用n个字符c构造一个siring类对象

使用举例:

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

int main()
{
	string str1;//无参构造
	string str2("hello world");//用字符数组构造
	string str3(10, 'c');//用10个字符c构造
	string str4(str2);//用另一个string类构造

	//打印
	cout << "str1:" << str1 << endl;
	cout << "str2:" << str2 << endl;
	cout << "str3:" << str3 << endl;
	cout << "str4:" << str4 << endl;
	return 0;
}

注:由于标准库已经实现了string类流插入与流提取的相关重载函数,所以我们可以直接配合cin和cout对string类进行输入和输出

析构函数(destructor)

string中字符串的内存是动态申请的,所以需要显示写析构释放相应空间。 析构函数已经被显示实现,我们无需多虑

赋值运算符重载operator=

c++11实现了五个赋值重载函数,其中最常用的当属前三种:

函数原型功能说明
string& operator=(const string& str);将一个string对象赋值给另一个string对象
string& operator=(const char* s);将一个字符数组赋值给一个string对象
string& operator=(char c);将一个字符赋值给一个string对象

我们可以看到,这些重载函数的返回值都是引用类型,不仅可以减少拷贝提升效率,还能够支持连续赋值

使用举例:

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

int main()
{
	string str1, str2, str3;

	str1 = "hello world";//字符串赋值
	str2 = str1;//string对象赋值
	str3 = 'c';//字符赋值

	//打印
	cout << "str1:" << str1 << endl;
	cout << "str2:" << str2 << endl;
	cout << "str3:" << str3 << endl;
	return 0;
}

 

二、string类的容量接口

string类有如下与容量相关的接口:

我们重点介绍一下较为常用的几个接口:

size和length

size和length的作用完全相同,都用于返回string对象中字符串的长度(不包括 '\0' ),单位是字节

使用举例:

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

int main()
{
	string str = "hello world";
	cout << str.size() << endl;
	cout << str.length() << endl;
}

看到这里,我们不禁发出疑问:为什么会实现两个功能完全相同的接口呢?由于string类之前是不属于STL的,只有一个接口length用于求字符串长度。但由于STL的其他容器都是通过size来求元素个数,为了保持一致性,string也实现了一个相同功能的接口size。所以相比其他容器,string的某些实现上就显得比较杂乱。

capacity

capacity返回string对象为字符串开辟的空间大小,单位是字节。

使用举例:

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

int main()
{
	string str = "hello world";
	cout << str.capacity() << endl;
}

我们在实现顺序表时,其结构中也有一个成员叫capacity,如果要插入数据,则会判断空间大小,空间不足则会提前申请空间供数组使用。string这里申请空间的机制是类似的。

clear

clear的作用是删除对象中字符串的有效内容,使其成为空字符串。注意:clear只会将有效字符清空,而不会改变空间容量的大小

使用举例:

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

int main()
{
	string str = "hello world";

	cout << "size:" << str.size() << endl;
	cout << "capacity:" << str.capacity() << endl << endl;

	str.clear();//清空字符串

	cout << "size:" << str.size() << endl;
	cout << "capacity:" << str.capacity() << endl;
}

empty

empty的作用是检测字符串是否是空串,如果是,则返回true;否则返回false。注意:该接口只是用于检查,不会修改字符串的任何内容

使用举例:

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

int main()
{
	string str = "hello world";

	cout << str.empty() << endl;

	str.clear();//清空字符串

	cout << str.empty() << endl;
}

 

reserve

reserve的作用是为字符串预留空间,单位是字节。reserve不会改变字符串有效字符的个数。注意:当参数n的值小于已有空间的总大小时,该函数不会改变其大小。

使用举例:

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

int main()
{
	string str = "hello world";

	cout << str.capacity() << endl;

	str.reserve(20);//增容

	cout << str.capacity() << endl;
}

我们可以看到,传参是20,但是空间容量却被增至31。标准规定增容的结果会大于等于参数n,具体会增至多少,取决于编译器

resize

resize的作用是将字符串中有效字符的个数修改为n个。当n大于原有的有效字符个数时,如果没有传第二个参数,则会用 '\0' 来填充元素,如果传入第二个参数c,则会用c来填充元素。注意:如果将元素个数增多,则空间大小可能改变;否则空间大小不会改变

使用举例:

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

int main()
{
	string str = "hello world";
	str.resize(20, 'm');

	cout << "size:" << str.size() << endl;
	cout << "capacity:" << str.capacity() << endl;
	cout << str << endl;
}

 

三、string对象的访问及遍历操作

1. string类的元素访问接口

        首先,我们介绍一下string类的元素访问相关接口,它们便于我们访问和修改字符串中的元素:

operator[ ]

operator[]是一个运算符重载,它能够让我们像访问数组元素一样访问string类字符串中的字符,因此,它的使用方法和数组的访问是相同的。注意:如果 pos 等于字符串长度,该函数将返回指向字符串最后一个字符之后的空字符的引用(该字符不应被修改)。

使用举例:

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

int main()
{
	string str = "hello world";

	cout << str[0] << endl;
	cout << str[3] << endl;
	return 0;
}

at

at的使用方法与operator[ ]相同,传入的参数对应字符串的下标。与operator[ ]不同的是:如果越界访问,at会抛出异常,而operator[ ]会返回 '\0' 的引用

使用举例:

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

int main()
{
	string str = "hello world";

	cout << str.at(0) << endl;
	cout << str.at(3) << endl;
	return 0;
}

front和back

顾名思义,front返回的是字符串的第一个字符,而back返回的是字符串中的最后一个字符(不是 '\0')

使用举例:

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

int main()
{
	string str = "hello world";

	str.front() = 'w';
	str.back() = 'w';

	cout << str << endl;
	return 0;
}

2. 迭代器接口

        由于迭代器(Iterator)的组成比较复杂,现阶段我们可以将其理解为一种指针,指向容器内的数据元素。它可以执行与指针一样的操作,能够让我们用“指针”的方式访问元素

string类的迭代器接口如下:

其中,比较常用的是前四种。

begin和end

begin返回一个指向字符串首字符的迭代器,而end返回指向字符串末尾 '\0' 的迭代器。它们返回的迭代器的类型是“iterator”或“const_iterator”(const对象)。对于一个容器而言,其迭代器接口都定义在类当中,所以我们定义迭代器时需要声明类域

前面已经提到,我们可以像指针般地使用迭代器访问数据元素。接下来我们尝试使用它:

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

int main()
{
	string str = "hello world";
	string::iterator it = str.begin();//定义一个string类的迭代器

	cout << *it << endl;//"解引用"
	it++;//后移一位
	cout << *it << endl;
}

 

在上面的程序中,我们可以看到定义迭代器时需要标明的类型比较冗长,这里有一个c++11小语法可以为我们提供方便:

auto关键字

        在c语言当中,auto所修饰的变量叫做“自动变量”,是具有自动存储器的局部变量。后来这个关键字并不显得十分重要,于是c++11赋予了auto全新的含义:auto关键字修饰的变量,其类型由编译器在编译时根据初始化值的类型推导而得。什么意思呢?我们看一段代码:

int main()
{
	auto a = 1;
	auto b = 5.5f;
	auto c = 'w';
    return 0;
}

调试窗口:

我们可以看到,这些根据我们赋的初值,自动推导出了相应的类型。所以今后对于那些十分冗长的类型,我们想要创建变量或对象时,就可以使用auto做修饰,编译器就会自动推导出其类型。

        使用auto关键字的注意事项

1. auto变量在定义时必须赋初值,否则编译器会报错。

2. 同一行声明多个auto变量时,所有初值的类型都必须相同

3. auto修饰指针类型时,使用auto或者suto*是没有区别的,但是修饰引用类型时必须加上 "&"

4. auto不能直接用于声明数组

5. auto不能用作函数形参,但可以用作返回值(谨慎使用)

这样,我们就可以用一个auto变量来接收迭代器接口的返回值了,无需标明冗长的类型名

rbegin和rend

rbegin和rend被成为“反向迭代器接口”,rbegin返回指向字符串最后一个字符的迭代器,rend返回指向字符串首元素“前一个位置”的迭代器。它们返回的迭代器类型是“reverse_iterator”或“const_reverse_iterator”(const对象)

使用举例:

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

int main()
{
	string str = "hello world";
	auto it = str.rbegin();

	cout << *it << endl;
	it++;
	cout << *it << endl;
	return 0;
}

这里要注意:对于反向迭代器,它的加减操作与普通迭代器相反

cbegin、cend、crbegin、crend

相比于前四种迭代器接口,这四种迭代器就是在之前的基础上修改为只能进行读操作,不可修改指向的值。这里就不多说了。 

3. string类字符串的三种遍历方法

        了解了string类的元素访问接口和迭代器相关知识,我们学习string类字符串的三种遍历方法:

3.1 下标访问

代码如下:

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

int main()
{
	string str = "hello world";
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << ' ';
	}
	cout << endl;
	return 0;
}

可以看到,程序通过循环产生下标,配合operator[ ]来遍历元素。 

3.2 迭代器访问

正向遍历:

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

int main()
{
	string str = "hello world";
	for (auto it = str.begin(); it != str.end(); it++)
	{
		cout << *it << ' ';
	}
	cout << endl;
	return 0;
}

反向遍历:

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

int main()
{
	string str = "hello world";
	for (auto it = str.rbegin(); it != str.rend(); it++)
	{
		cout << *it << ' ';
	}
	cout << endl;
	return 0;
}

3.3 范围for

        由于string类字符串是一个有范围的集合,我们可能会经常对其进行遍历操作,但是使用下标访问或者迭代器访问的方式,每次遍历都需要写明范围,并且有时还会出现错误。对于这种问题,c++11规定了一种新语法:范围for循环语句。它的使用方法是:

for((范围内用于迭代的变量) : (被迭代的范围))

{

        ...
}

范围for会自动判断遍历结束,每一次循环结束后,用于迭代的变量都会更新

范围for底层就是由迭代器实现的。一个容器支持迭代器,那么就支持范围for

接下来我们尝试使用范围for遍历字符串:

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

int main()
{
	string str = "hello world";
	for (auto e : str)//使用范围for遍历
	{
		cout << e << ' ';
	}
	cout << endl;
	return 0;
}

四、string类的修改操作接口

        string类中字符串内容修改相关的接口如下:

其中operator+=最为常用,我们重点介绍;其他接口作为了解即可。

operator+=

operator+=是一个运算符重载,它的作用是字符串追加,可以追加字符,也可以追加字符串

它的常用重载函数有三个:

函数原型功能说明
string& operator+=(const string& str);追加一个对象中的字符串
string& operator+=(const char* s);追加一个字符数组
string& operator+=(char c);追加一个字符

使用举例:

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

int main()
{
	string str1 = "hello ";
	string str2 = "world";

	cout << str1 << endl;

	str1 += str2;//追加一个对象中的字符串
	cout << str1 << endl;

	str1 += '!';//追加一个字符
	cout << str1 << endl;

	str1 += "!!!!";//追加一个字符数组
	cout << str1 << endl;

	return 0;
}

过一会我们了解push_back、append时,你会发现它们的一些接口与operator+=的效果是一样的。不过一般情况下,operator+=比较常用。

小技巧:我们在对字符串进行追加等操作时,如果能够大概预估到需要多大空间,可以先使用reserve将空间预留好,然后进行操作,可以提升运行效率。

append

 append作用是追加字符串,它有七个重载函数

(1)string
        追加一个str的副本。
(2)substring
        追加一个str的子字符串的副本。子字符串是str的一部分,从字符位置subpos开始,并跨越子字符(或者直到str的末尾,如果str太短或者sublen是string::npos)。
(3) c-string
        附加一个由s指向的以空结尾的字符序列(C-string)组成的字符串的副本。
(4)buffer
        在s所指向的字符数组中追加前n个字符的副本。
(5)fill
        追加n个字符c的连续副本。
(6)range
        以相同的顺序追加[first,last)范围内字符序列的副本。
(7)initializer list
        以相同的顺序追加il中每个字符的副本。

push_back和pop_back

push_back的作用是将一个字符追加到一个字符串末尾。 

pop_back的作用是删除字符串末尾的字符。 

assign

assign的作用是给一个字符串赋一个新值,舍弃原来的值。他有八个重载函数

(1)string
        str副本。
(2)substring
        复制str中从字符位置subpos开始的部分,并跨出sublen字符(如果str太短或sublen为string::npos,则复制到str的末尾)。
(3) c-string
        复制以s为指向的以空结尾的字符序列(C - string)。
(4)buffer
        从s指向的字符数组中复制前n个字符。
(5)fill
        将当前值替换为字符c的连续n个副本。
(6)range
        以相同的顺序复制[first,last)范围内的字符序列。
(7)initializer list
        以相同的顺序复制il中的每个字符。
(8)move
        获取str的内容。STR保持在未指定但有效的状态。

insert

insert的作用是在字符串指定位置插入字符或者字符串。八个重载函数描述如下:

(1)string
        插入一个str的副本。
(2)substring
        插入str的子字符串的副本。子字符串是str的一部分,从字符位置subpos开始,并跨越子字符(或者直到str的末尾,如果str太短或sublen为npos)。
(3) c-string
        插入由以s为指向的以空结尾的字符序列(C-string)组成的字符串的副本。
(4)buffer
        在s所指向的字符数组中插入前n个字符的副本。
(5)fill
        插入n个字符c的连续副本。
(6)single character
        插入字符c。
(7)range
        以相同的顺序在[first,last)范围内插入字符序列的副本。
(8)initializer list
        以相同的顺序插入il中每个字符的副本。

erase

erase的作用是删除字符串的一部分,三个重载函数如下:

(1)sequence
        擦除字符串值中从字符位置pos开始并跨越len字符的部分(如果内容太短或len为string::npos,则擦除直到字符串末尾)。注意,默认实参会擦除字符串中的所有字符(类似于成员函数clear)。
(2)character
        擦除指向p的字符。
(3)range
        擦除[first,last)范围内的字符序列。

replace

replace可以将字符串的一部分替换为另一字符串或者字符。这个接口听着感觉比较实用,但是当需要替换的部分的长度比原部分大时,就需要将后面的字符全体后移,效率较低,实用性较差

七个重载函数:

(1)string
        str副本。
(2)substring
        复制str中从字符位置subpos开始的部分,并跨出sublen字符(如果str太短或sublen为string::npos,则复制到str的末尾)。
(3) c-string
        复制以s为指向的以空结尾的字符序列(C-string)。
(4)buffer
        从s指向的字符数组中复制前n个字符。
(5)fill
        将字符串的一部分替换为字符c的连续n个副本。
(6)range
        以相同的顺序复制[first,last)范围内的字符序列。
(7)initializer list
        以相同的顺序复制il中的每个字符。

swap

顾名思义,swap的功能是交换两字符串的内容。使用举例:

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

int main()
{
	string str1 = "aaaaa";
	string str2 = "bbbbbb";
	str1.swap(str2);//交换两字符串

	cout << "str1:" << str1 << endl;
	cout << "str2:" << str2 << endl;
}

五、string类的字符串运算相关接口

        string类有以下字符串运算相关的接口(博主只介绍其中比较常用的几个,其他的可自行了解):

c_str

c_str可以将对象临时转换为一个字符数组(以 '\0' 结尾)。它的返回值是指向自己成员字符数组的指针

该函数返回的指针指向内容被const修饰,我们只能读,不能通过该函数修改字符串内容

使用举例:

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

int main()
{
	string str = "hello world";

	cout << str.c_str() << endl;//打印字符串

	return 0;
}

find和rfind 

find用于查找一个字符串当中第一次出现的子串或者字符。 如果给定了参数pos,则函数会从字符串的pos位置开始向后查找;否则从起始处查找

如果找到了,函数会返回第一个匹配的第一个字符的位置。如果找不到,则返回npos(第六点有介绍)

这里解释一下第三个重载函数:从pos位置开始,查找对象字符串中的第一个匹配的子串,该子串由字符串s中前n个字符构成

与find相反,rfind的作用是查找一个字符串当中最后一次出现的子串或者字符如果给定了参数pos,则函数会从字符串的pos位置开始向前查找;否则从起始处查找。其余机制与find相同,不多做赘述。

substr

 substr会产生一个新的string对象,该对象中的字符串是由原字符串中pos位置开始的len个字符组成如果没有给定参数len,则一直截取到原字符串末尾

使用举例:

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

int main()
{
	string str1 = "hello world";
	string str2 = str1.substr(2);//截取下标为2的位置开始的部分
	string str3 = str1.substr(2, 6);//截取下标为2的位置开始的六个字符

	cout << str2 << endl;
	cout << str3 << endl;
	return 0;
}

六、string类的常量成员

刚才我们在一些接口的缺省参数中看到了"npos",它便是string类的常量成员。

npos具有size_t类型元素的最大可能值

当在string的成员函数中作为len(或sublen)参数的值时,该值表示“直到字符串结束”。

作为返回值,它通常用于表示没有匹配。

该常量定义为-1,由于size_t是无符号整型,因此它是该类型的最大可表示值。

七、string类的非成员函数

最后,我们介绍几个string类相关的非成员函数,它们在string的使用中有至关重要的作用。

relational operators(关系运算符重载)

 string类重载了一系列关系运算符,用于比较对象之间的大小。它们的比较机制与c语言中的strcmp相同。我们使用这些运算符时,只需要将两对象放在运算符两边即可。

使用举例:

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

int main()
{
	string str1 = "abc";
	string str2 = "abd";
    
    //大小比较
	if (str1 > str2)
	{
		cout << "str1 > str2" << endl;
	}
	else if (str1 == str2)
	{
		cout << "str1 == str2" << endl;
	}
	else
	{
		cout << "str1 < str2" << endl;
	}
	return 0;
}

operator>>和operator<<

 这两个运算符重载用于对string的输入和输出。使用方法与其他内置类型的输入输出相同

对于输入操作,函数会以空格、回车等字符作为分隔符,它们将无法读入字符串当中。如果想要读取这些字符,就需要使用getline函数

getline

getline的作用是从输入流中提取字符串并将其存储到str中,直到找到分隔字符delim(没有给定该参数时,分隔符默认是 '\n' )。如果到达文件的末尾,或者在输入操作期间发生其他错误,提取也会停止如果找到分隔符,则提取并丢弃它(即不存储它,下一个输入操作将在它之后开始)

注意:调用之前str中的任何内容都将被新提取的序列替换

使用该函数输入时,每个提取的字符都被附加到字符串中,就像调用了其成员push_back一样。

使用举例:

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

int main()
{
	string str1;
	getline(cin, str1);//使用getline输入整行
	cout << str1 << endl << endl;

	string str2;
	cin >> str2;//使用operator>>输入
	cout << str2 << endl;
	return 0;
}

 

可以看到,getline读取了一整行字符,而operator>>遇到空格就停止读取

swap 

成员函数当中也有一个swap接口,用于两个字符串的交换这个非成员的swap作用于其相同,只是在调用方法上略有不同而已

总结

        今天,我们正式进入了STL的大门,学习了第一个容器——string。string虽然接口较多,但是它的实用性很高,为我们处理字符串提供了很多便利。由于其中一些接口并不是很常用,博主就没有过多赘述,大家可以在cplusplus官网进一步了解它们。之后博主会和大家一起,在熟练运用的基础上,深入其底层,并尝试模拟实现string类。如果你觉得博主讲的还不错,就请留下一个小小的赞在走哦,感谢大家的支持❤❤❤


http://www.kler.cn/news/356919.html

相关文章:

  • 如何在博客中插入其他的博客链接(超简单)最新版
  • C语言刷题 LeetCode 删除单链表的重复节点 双指针法
  • 创客项目秀|基于XIAO ESP32C3的本地个人助理Mr.M
  • 突然猫毛过敏了怎么办?宠物空气净化器高效处理猫毛!
  • 关于目前面试八股文的一些心得体会
  • LeetCode 643.子数组最大平均数 I
  • SQL字段类型全解析:知识点、应用场景与长度说明
  • mysql多表关系与查询
  • MySQL 【日期】函数大全(七)
  • 深圳出手!新能源汽车被针对了
  • Android 取消充电动画logo,直接显示图片即可
  • linux线程 | 全面理解同步与互斥 | 同步
  • python+docxtpl:word文件模版渲染
  • 近期股市热潮,现有架构模块下金融交易系统如何应对“冲击”?优化思路如下
  • package.json 里的 dependencies和devDependencies区别
  • C++游戏开发:从零开始构建一个简单的2D平台游戏《跳跃冒险》
  • 从头预训练一只迷你 LLaMA 3_llama3 预训练预处理
  • apifox发送请求,参数类型为枚举类
  • 力扣——环形链表(链表)C语言
  • 浏览器无法安全下载文件怎么解决