C++入门 string常用接口(中)

目录

string类的常用接口说明

string类对象的容量操作  

 size & max_size & length & capacity

empty & clear 

reserve & resize

 string类对象的元素访问

 at & back & front

string类对象的修改操作(字符串操作)

substr & find & rfind

find_first_of & find_last_of

find_first_not_of & find_last_not_of

compare


string类的常用接口说明

string类对象的容量操作  

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

 size & max_size & length & capacity

void test_string7()
{
	string s1 = "hello world";
	cout << s1.size() << endl;		//输出11
	cout << s1.length() << endl;	//输出11
	cout << s1.capacity() << endl;  //输出15

	string s2;
	cout << s2.size() << endl;		//输出0
	cout << s2.length() << endl;	//输出0
	cout << s2.capacity() << endl;  //输出15
}

这三个相当简单,不过多赘述,注意的是size返回字符串有效字符长度(不包括\0)

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

max_size返回字符串可以达到的最大长度,没啥用处,不用深度学习。


empty & clear 

void test_string8()
{
	string s1 = "hello world";
	if (s1.empty())//输出no
		cout << "yes" << endl;
	else
		cout << "no" << endl;
	s1.clear();
	if (s1.empty())//输出yes
		cout << "yes" << endl;
	else
		cout << "no" << endl;
}

empty函数判度字符串是否为空,clear函数清空字符串。

clear()只是将string中有效字符清空,不改变底层空间大小。


reserve & resize

在了解这两个函数之前先了解编译器是怎么存储string类的数据的:

vs下string的结构

  1. string总共占28个字节,内部结构稍微复杂一点,先是有一个联合体,联合体用来定义string中字 符串的存储空间:
  2. 当字符串长度小于16时,使用内部固定的字符数组来存放 ;当字符串长度大于等于16时,从堆上开辟空间。

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

g++下string的结构

  1. G++下,string是通过写时拷贝实现的,string对象总共占4个字节,内部只包含了一个指针,该指针将来指向一块堆空间 。

 

reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于 string的底层空间总大小时,reserve不会改变容量大小。 再三强调reserve和reverse的拼写区别!!

对string操作时,如果能够大概预估到放多少字符,可以先通过reserve把空间预留好。

void TestPushBack()
{
	string s;
	// 知道需要多少空间,提前开好
	s.reserve(200);
	//s[100] = 'x'; //无法赋值
}

 注意:这里的开辟了200个空间只是提前预留好的,不能进去访问,否则会出错

void test_string9()
{
	string s1;
	// reserve 保留
	// reverse 逆置 反转
	s1.reserve(100);
	cout << s1.capacity() << endl;

	s1.reserve(20);
	cout << s1.capacity() << endl;
}

 另外注意不同编译器下reserve的处理方式不同,可能会缩容也可能不会。

 

如果要缩容,也只是另外开辟一半的空间,将原先空间的值拷贝一半进去,不能够在原来空间上缩容,归根结底还是空间换时间。


resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间

void test_string11()
{
	string s1;
	//s1.resize(5, '0');
	s1.resize(5);
	s1[4] = '3';
	s1[3] = '4';
	s1[2] = '5';
	s1[1] = '6';
	s1[0] = '7';
	cout << s1 << endl;
	// 76543

	// 插入(空间不够会扩容)
	string s2("hello world");
	s2.resize(20, 'x');
	cout << s2 << endl;
	//hello worldxxxxxxxxx

	// 删除
	s2.resize(5);
	cout << s2 << endl;
	//hello
}

注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。

 string类对象的元素访问

operator [ ]在上文已经详细解释,不再过多描述。 

 at & back & front

at 能获取字符串中的字符,返回对字符串中位置 pos 处的字符的引用。

void test_string12()
{
	string s2("hi");
	//  该函数会自动检查 pos 是否是字符串中字符的有效位置
	// (即 pos 是否小于字符串长度)
	//  如果不是,则抛出out_of_range异常。
	try
	{
		s2.at(10);//非法访问
	}
	catch (const exception& e)
	{
		cout << e.what() << endl;
		//输出 invalid string position
	}
}

front 访问第一个字符,back 访问最后一个字符

void test_string13()
{
	string s = "hello world";
	s.back() = 'D';
	s.front() = 'H';

	cout << s << endl;
	//输出Hello worlD
}

string类对象的修改操作(字符串操作)

substr & find & rfind

void test_string14()
{
	string file("string.cpp.zip");
	
	// 在字符串中搜索其参数指定的序列的第一次出现项
	size_t pos1 = file.find('.');

	// 在字符串中搜索其参数指定的序列的最后一次出现项
	size_t pos2 = file.rfind('.');

	// 子字符串是对象中从字符位置开始并跨越字符(或直到字符串末尾,以先到者为准)的部分
	// string suffix = file.substr(pos, file.size() - pos);
	string suffix1 = file.substr(pos1);	//默认到结束
	cout << suffix1 << endl;//输出  .cpp.zip

	string suffix2 = file.substr(pos2);
	cout << suffix2 << endl;//输出  .zip

	string suffix3 = file.substr(pos1,4);//从pos1开始跨越四个字符
	cout << suffix3 << endl;//输出  .cpp
}

注意:substr取的范围为左闭右开区间 

find_first_of & find_last_of

void test_string15()
{
	// strtok
	std::string str("Please, replace the vowels in this sentence by asterisks.");
	std::size_t found1 = str.find_first_of("aeiou");//在字符串中搜索与其参数中指定的任何字符匹配的第一个字符
	std::size_t found2 = str.find_last_of("aeiou");//在字符串中搜索与其参数中指定的任何字符匹配的最后一个字符

	str[found1] = '*';
	str[found2] = '*';

	std::cout << str << '\n';
	//输出 Pl*ase, replace the vowels in this sentence by aster*sks.
}

find_first_not_of & find_last_not_of

void test_string16()
{
	// strtok
	std::string str("Please, replace the vowels in this sentence by asterisks.");
	std::size_t found1 = str.find_first_not_of("aeiou");//在字符串中搜索与其参数中指定的任何字符匹配的第一个字符
	std::size_t found2 = str.find_last_not_of("aeiou");//在字符串中搜索与其参数中指定的任何字符匹配的最后一个字符

	str[found1] = '*';
	str[found2] = '*';

	std::cout << str << '\n';
	//输出 *lease, replace the vowels in this sentence by asterisks*
}

compare

void test_string17()
{
	string str1("green apple");
	string str2("red apple");

	if (str1.compare(str2) != 0)
		std::cout << str1 << " is not " << str2 << '\n';
	// 输出 green apple is not red apple
}

C++98设置的类型过多,个人绝对记忆太过于繁琐,只需要了解几个有用的即可,比较是按照字典序大小比较的(ascii码比较

copy & c_str

将字符串对象的当前值的子字符串复制到 s 指向的数组中,此子字符串包含从位置 pos 开始的 len 字符。

void test_string18()
{
	char buffer[20];
	string str("Test string...");
	size_t length = str.copy(buffer, 6, 5);
	buffer[length] = '\0';
	cout << "buffer contains: " << buffer << '\n';
	//输出 buffer contains: string
}

void test_string25()
{
	string str = "Please split this sentence into tokens";
	cout << str.c_str() << endl;   // 以C语言的方式打印字符串
}
  • 40
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值