Quickly understand the usage of string in STL

An obvious advantage of c++ compared to c language is STL. There are many ready-made data structures in c++, and we can use them to solve problems very conveniently. Next we introduce a new member: string

What is the string class?

First of all, we can take a look at the documentation about the string class: Introduction to the documentation of the string class

Summary: 1. Simply put, it is a class that represents strings, and performs corresponding operations on strings by calling different encapsulated interfaces.

2. String is actually an alias of the basic_string template class at the bottom layer, typedef basic_string<char, char_traits, allocator>

3. When using the string class, you need to include the header file #include and using namespace std.

Common calling interface of string class

    • Common constructions of string class objects

( construct ) function construction

Function Description

string() (emphasis)

string(const char* s) (emphasis)

Constructed with a string in c format

string(size_t n, char c)

The string class object contains n characters c

string(const string&s) (emphasis)

copy constructor

Usage display:

2. Capacity operation of string objects

function name

Function Description

size (emphasis)

Returns the effective length of the string

length

Returns the effective length of the string

capacity

Returns the total size of the space

empty (emphasis)

Determine whether the string is empty, return true if it is, return false if it is not

clear(重点)

清空字符串

reserse(重点)

为字符串预留空间

resize(重点)

将字符的个数改为n个,多出的空间用字符填充

q:这时就会有人困惑了,size和length的功能一样,为什么要设计两个不同的函数呢??

答:在我们后面会学习不同类型的数据结构如vector、list、map等等,显然有的不适合用length来封装容器中数据的有效个数,使用size是为了保证接口的一致性。

用法展示:


注:clear函数只清除有效字符,不改变容量的大小。

注:reserve函数的功能是提前预留空间,但空间的大小并不能根据你给的值而定,是编译器根据你给的空间的小而预留出合适的空间大小。另外,reserve函数只扩容不缩容!!



通过图我们可以分为以下3种情况:

  1. 当resize中参数的值<size()函数返回的值,我们可以理解成删除数据,即把数据删成只剩下resize中的参数值。

  1. 当resize中参数的值>=size()函数返回的值&& 当resize中参数的值<=capacity()函数返回的值,我们可以理解成只是改变size的大小不改变capacity的大小,如果resize()中的参数指定了某个字符,那么原数据中末尾会补齐这个特定的字符直到字符串达到size的大小,不写则默认不补。

3.当resize中的参数值>capacity()函数返回的值时,我们就可以理解成扩容,类似于reserve函数。

string类对象的访问及遍历操作

函数名称

功能说明

operator[](重点)

返回pos位置的字符(支持随机访问)

begin+end

begin获取第一个字符迭代器,end获取最后一个字符的下一个位置的迭代器

rbegin+rend

反向迭代器

范围for

c++11支持

下标的随机访问:

迭代器遍历:

string类对象的修改操作

函数名称

功能说明

push_back

尾插字符

append

追加一个字符串

operator+=(重点)

追加字符串

c_str(重点)

返回c格式字符串

find+npos(重点)

从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置

rfind

从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置

substr

在str中从pos位置开始,截取n个字符,然后将其返回

代码演示:


使用find函数时如果找得到字符就返回该字符所在位置(位置下标),找不到时就返回npos,npos的值为-1。substr函数是截取字符串。

string类非成员函数

函数名称

功能

operator+

尽量少用,传值返回,导致深拷贝效率低

operator>>(重点)

输入运算重载符号

operator<<(重点)

输出运算重载符号

getline(重点)

获取一行字符串

relational operators(重点)

大小比较

swap

字符串交换

getline的用法:

relational operators的用法:

其余接口非常简单咱就不一一多说了。

到现在位置我们string类学的就差不多了,我讲解的是平常用的比较多以及比较重要的函数接口,如有什么错误请大家给我指正出来我一定虚心请教~~感谢老铁支持!!

Guess you like

Origin blog.csdn.net/m0_69005269/article/details/128548729