【C++】-8- string〔常见接口函数使用〕

本文详细介绍了C++标准库中的string类,包括其用于构造字符串的不同方法,如空构造、字符数组构造和拷贝构造。还讨论了string对象的修改操作,如追加字符和字符串、查找和替换功能。此外,提到了容量操作如size、length、capacity、clear、reserve和resize,以及访问和遍历string对象的方法。最后,文章提到了string类的迭代器、at函数和swap函数的使用。
摘要由CSDN通过智能技术生成

概览

在这里插入图片描述

标准库中的string类

string(cplusplus.com)

「string类(了解)」

在这里插入图片描述

  1. 字符串是表示字符序列的类
  2. 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作单字节字符字符串的设计特性。
  3. string类是使用char(即作为它的字符类型,使用它的默认char_traits和分配器类型(关于模板的更多信息,请参阅 basic_string)。
  4. string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits和allocator作为basic_string的默认参数(根于更多的模板信息请参考 basic_string)。
  5. 注意,这个类独立于所使用的编码来处理字节:如果用来处理多字节或变长字符(如UTF-8)的序列,这个类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作。

﹝编码﹞

字符编码(英语:Character encoding)也称字集码,是把字符集中的字符编码为指定集合中某一对象(例如:比特模式、自然数序列、8位组或者电脉冲),以便文本在计算机中存储和通过通信网络的传递。(字符编码—百度百科)

在这里插入图片描述

ASCII:(American Standard Code for Information Interchange)美国信息交换标准代码
GBK:GBK全称《汉字内码扩展规范》(GBK即“国标”、“扩展”汉语拼音的第一个字母,英文名称:Chinese Internal Code Specification)
在这里插入图片描述在这里插入图片描述

「string类的常用函数接口」

﹝1.string类对象的常见构造﹞

在这里插入图片描述

函数名称功能说明
string()构造空的string类对象,即空字符串
string(const char* s)用 常量字符串 来构造string类对象
string(size_t n, char c)用 n 个字符 c 来构造string类对象
string(const string& s)拷贝构造函数

示例:

#include <string>
using namespace std;
string s1;//调用无参的构造函数,构造空的string类对象
string s2("hello RoundBottle");//用常量字符串构造string类对象
string s3(13, 'x');//用 13个x 构造 string类对象
string s4(s2);//拷贝构造

在这里插入图片描述
注意:string 类对象支持直接用 cin 和 cout 进行输入和输出

string s("hello, RoundBottle!");
cout << s << endl;

﹝2.string类对象的修改操作﹞

函数名称功能说明
push back在字符串后尾插字符
append在字符串后追加一个字符串
operator+=在字符串后追加字符串str
c_str返回C格式字符串
find + npos从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置
rfind从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
substr在str中从pos位置开始,截取n个字符,然后将其返回
  • 注意:
  1. 在string尾部追加字符时,s.push_back( c ) / s.append(1, c) / s += 'c’三种的实现方式差不多,一般情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可以连接字符串。
  2. 对string操作时,如果能够大概预估到放多少字符,可以先通过 reserve(下面会介绍)把空间预留好。
  • 示例:
    在这里插入图片描述
‹ c_str ›

std::string::c_str
const char* c_str() const;

在这里插入图片描述

ps. cout 会自动识别类型,c_str() 函数的返回值是 const char* ,强制转成 void* 才会打印地址 在这里插入图片描述

c_str接口的意义:为了和C语言的接口兼容

‹ npos ›

public static member constant

static const size_t npos = -1; Maximum value for size_t
size_t无符号整型→ -1 本质上是整型的最大值!

‹ string结构 ›
  • vs下string的结构:先是有一个联合体,联合体用来定义string中字符串的存储空间:
    当字符串长度小于16时,使用内部固定的字符数组 (buf[16]) 来存放
    当字符串长度大于等于16时,从堆上开辟空间

在这里插入图片描述
在这里插入图片描述
故:8 + 8 + 8 + 16 = 40字节(如上图所示)

这种设计也是有一定道理的,大多数情况下字符串的长度都小于16,那string对象创建好之后,内部已经有了16个字符数组的固定空间,不需要通过堆创建,效率高。
在这里插入图片描述

vs 一般是1.5倍扩容

  • g++下string的结构
    g++下,string是通过写时拷贝实现的,string对象总共占4个字节,内部只包含了一个指针,该指针将来指向一块堆空间,内部包含了如下字段:
    • 空间总大小
    • 字符串有效长度
    • 引用计数
    • 指向堆空间的指针,用来存储字符串。

g++ 一般是2倍扩容


﹝3.string类对象的容量操作﹞

在这里插入图片描述

函数名称功能说明
size返回字符串有效长度
length返回字符串有效长度
capacity返回空间的总大小(不包括\0)
empty检测字符串是否为空串,是返回true,否则返回false
clear清空有效字符
reserve为字符串预留空间
resize将有效字符的个数改成n个,多出的空间用字符c填充
  • size 与 length

ps. size()length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()

  • size && capacity
    在这里插入图片描述
‹ clear ›

清空 string 的内容,size 置零
在这里插入图片描述

‹ reserve ›

std::string::reserve
void reserve (size_t n = 0);
Request a change in capacity
Requests that the string capacity be adapted to a plannedchange in size to a length of up to n characters.

If n is greater than the current string capacity, the function causes the container to increase its capacity to n characters (or greater).

In all other cases, it is taken as a non-binding request to shrink the string capacity: the container implementation is free to optimize otherwise and leave the string with a capacity greater than n.

This function has no effect on the string length and cannot alter its content.

  • 对于函数所传参数size_t n
    • 默认缺省值为0
    • n > current string capacity :该函数会导致容器将其容量增加到 n 个字符(或更大)
    • n <= current string capacity :容器实现可以自由优化,否则,字符串的容量大于 n。

在这里插入图片描述

‹ resize ›

std::string::resize
void resize (size_t n);
void resize (size_t n,char c);
Resize string
Resizes the string to a length of n characters.

If n is smaller than the current string length, the current value isshortened to its first n character, removing the characters beyond the nth.

If n is greater than the current string length, the current content is extended by inserting at the end as many characters as needed to reach a size of n. If c is specified, the new elements are initialized as copies of c, otherwise, they are value-initialized characters (null characters).

resize:将字符串大小调整为 n 个字符的长度

  • n > current string length :删除 n 个字符长度以后的数据
    在这里插入图片描述
  • n >= current string length :在末尾插入所需数量的字符以达到 n 大小来扩展当前内容
    在这里插入图片描述

﹝4.string类对象的访问及遍历操作﹞

函数名称功能说明
operator[]返回pos位置的字符,const string类对象调用
begin+ endbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
rbegin + rendbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
范围forC++11支持更简洁的范围for的新遍历方式
void test7()
{
	string s("abcdefg");

	string::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

在这里插入图片描述

注:范围for底层也是迭代器

string s("abcdefg");
for (auto ch : s)
{
	cout << ch << ' ';
}
cout << endl;

﹝5.一些补充﹞

「interator」

使用示例:

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

int main()
{
	string s1("Hello RoundBottle");
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	string s2("Hello fantasy");
	string::reverse_iterator rit = s2.rbegin();
	while (rit != s2.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;

	return 0;
}

在这里插入图片描述

  • const iterator:只读
「at」
std::string::at
char& at (size_t pos);const char& at (size_t pos) const;

关于越界访问:(以100为例)
string s[100]:越界异常直接终止程序(assert)
s.at(100):越界异常,抛异常

总之,实际中很少会用,功能和 [ ] 差不多

「swap」
  • 只针对string:
    std::string:: swap
    void swap (string& str);

  • 对于所有类型都适用:
    std:: swap (string)
    void swap (string& x, string& y);

以上两个函数的结果都是一样的

  • 使用示例:
#include<iostream>
using namespace std;
#include<string>
int main()
{
	string s1("Hello RoundBottle");
	string s2("Hello fantasy");
	s1.swap(s2);

	cout << "s1:" << s1 << endl;
	cout << "s2:" << s2 << endl;

	return 0;
}
#include<iostream>
using namespace std;

int main()
{
	string s1("Hello RoundBottle");
	string s2("Hello fantasy");
	swap(s1,s2);

	cout << "s1:" << s1 << endl;
	cout << "s2:" << s2 << endl;

	return 0;
}

END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

畋坪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值