String类应用

 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)的序列,这个类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作

总结:

  1. string是表示字符串的字符串类

  2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。

使用strin

string类对象的常见构造
(constructor)函数名称功能说明
string() (重点)构造空的string类对象,即空字符串
string(const char* s) (重点)用C-string来构造string类对象
string(size_t n, char c)string类对象中包含n个字符c
string(const string&s) (重点)拷贝构造函数
string类对象的容量操作
函数名称功能说明
size(重点)返回字符串有效字符长度
length返回字符串有效字符长度
capacity返回空间总大小
empty (重点)检测字符串释放为空串,是返回true,否则返回false
clear (重点)清空有效字符
reserve (重点)为字符串预留空间**
resize (重点)将有效字符的个数该成n个,多出的空间用字符c填充

注意:

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

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

  3. resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字 符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的 元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大 小,如果是将元素个数减少,底层空间总大小不变。

  4. reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于 string的底层空间总大小时,reserver不会改变容量大小。

string类对象的访问及遍历操作
函数名称功能说明
operator[] (重 点)返回pos位置的字符,const string类对象调用
begin+ endbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭 代器
rbegin + rendbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭 代器
范围forC++11支持更简洁的范围for的新遍历方式
string类对象的修改操作
函数名称功能说明
push_back在字符串后尾插字符c
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把空间预留好。

string类非成员函数
函数功能说明
operator+尽量少用,因为传值返回,导致深拷贝效率低
operator>> (重点)输入运算符重载
operator<< (重点)输出运算符重载
getline (重点)获取一行字符串
relational operators (重点)大小比较
接口演示一
#include<iostream>
#include<string>
using namespace std;
void test_string1()
{
	string s1;
	string s2("hello world");//构造函数
	string s3(s2);//拷贝构造
	cout << s3 << endl;//重载了流插入运算符

	cin >> s1;
	cout << s1 << endl;

	string ret1 = s1 + s2;//重载了+和=运算符
	cout << ret1 << endl;

	string ret2 = s1 + "666";
	cout << ret2 << endl;
}
void test_string2()
{
	string s1("hello world");
	string s2 = "hello C++";

	//遍历string的方法
	for (size_t i = 0; i < s1.size(); i++)
	{
		//读功能
		cout << s1[i] << " ";
	}
	cout << endl;
	
	for (size_t i = 0; i < s1.size(); i++)
	{
		//写功能
		s1[i]++;//重载了++运算符
	}
	cout << s1 << endl;

	//迭代器
	string::iterator it = s1.begin();
	//while(it < s1.end()) //这里可以但不建议,如果是链表,空间不连续就会失败
	while (it != s1.end()) //推荐玩法,通用
	{
		//读功能
		cout << *it << " ";
		++it;
	}
	cout << endl;
	
	it = s1.begin();
	while (it != s1.end())
	{
		//写功能
		*it = 'a';
		++it;
	}
	cout << endl;
	cout << s1 << endl;

}
void test_string3()
{
	string s1("hello world");
	string::iterator it = s1.begin();

	//string::reverse_iterator rit = s1.rbegin();//类型名太长
	auto rit = s1.rbegin(); //可以运用auto自动识别rbegin类型
	while (rit != s1.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;

	//原理:编译器替换成迭代器
	for (auto ch : s1)
	{
		//读
		cout << ch << " ";
	}
	cout << endl;
=	for (auto ch : s1)
	{
		ch++;
	}
	cout << endl;
	cout << s1 << endl;
}
int main()
{
	//test_string1();
	//test_string2();
	test_string3();
	return 0;
}
#include<iostream>
using namespace std;
void func(const string& s)
{
	auto it = s.begin();
	while (it != s.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
}
void test_string4()
{
	string s1("hello worldxxxxxxxxxxxxxx");
	func(s1);

	string s2(s1);
	cout << s2 << endl;

	string s3(s1, 6, 5);//部分拷贝字符串s1下标6位置并且向后拷贝5个字符
	cout << s3 << endl;

	string s4(s1, 6); //默认拷贝字符串s1下标6位置并且向后拷贝到结尾
	cout << s4 << endl;

	string s6(s1, 6, s1.size() - 1);
	cout << s6 << endl;

	string s7(10, 'a');
	cout << s7 << endl;

	string s8(++s7.begin(), --s7.end());
	cout << s8 << endl;

	s8 = s7;
	s8 = "xxx";
	s8 = 'y';
}
void test_string5()
{
	string s1("hello worldxxxxxxxxxxxxxxxxxxxxxxxxx");
	cout << s1.size() << endl;
	cout << s1.length() << endl;
	cout << s1.capacity() << endl;

	s1.clear(); //将数据清空但内存空间保留
	s1 += "张三";
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;

	string filename;
	cin >> filename;
	FILE* fout = fopen(filename.c_str(), "r"); //契合C的写法返回C格式字符串
}
int main()
{
	//test_string4();
	test_string5();
	return 0;
}
接口演示二
#include<iostream>
#include<string>
using namespace std;
void test_string6()
{
	string s;
	//s.reserve(100); 提前进行开辟空间,减少扩容,提高效率
	//保留100个字节的空间

	size_t old = s.capacity();
	cout << "初始化:" << s.capacity() << endl;

	for (size_t i = 0; i < 100; i++)
	{
		s.push_back('x');
		if (s.capacity() != old)
		{
			cout << "扩容:" << s.capacity() << endl;
			//VS下接近1.5倍每次,Linux接近2倍每次
			old = s.capacity();
		}
	}
	/*s.reserve(10);*/  //缩小容量,但只是编译器发出不具约束力的请求,一般是不会缩
	cout << s.capacity() << endl; 
}
void test_string7()
{
	string s1("hello world");
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;

	//resize > capacity 扩容再改变size,并\0或自定字符填充字母

	//size < resize() < capacity 增大size并以\0来填充或者以自定的字符填充

	s1.resize(20, 'x');
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;

	s1.resize(5);//resize() < size,resize就是删除作用
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;

	string s2;
	s2.resize(10, '#');
	cout << s2 << endl;
	cout << s2.size() << endl;
	cout << s2.capacity() << endl;

	s2[0]++;
	s2.at(0)++;
	//意义相同,at如果越界了会抛异常
	//[]是断言的,越界了直接中断程序
	cout << s2 << endl;
}
void test_stirng8()
{
	string ss("world");

	string s;
	s.push_back('#');
	s.append("hello");//尾部插入
	s.append(ss);
	cout << s << endl;

	s += '#';
	s += "hello";
	s += ss;
	cout << s << endl;

	string ret1 = ss + '#';
	string ret2 = "hello" + ss;
	cout << ret1 << endl;
	cout << ret2 << endl;

}
void test_string9()
{

	string str("xxxxxxxxx");
	string base = "The quick brown fox jumps over a lazy dog.";
	//相当于赋值
	str.assign(base);
	cout << str << endl;

	//从第五个字符赋值到十个字符
	str.assign(base, 5, 10);
	cout << str << endl;

}
void test_string10()
{
	//insert/erase/replace 能不用就尽量不用,因为他们都设计挪动数据,效率不高
	//接口设计复杂繁多,需要时查一下文档

	string str("hello world");
	
	//在开头的位置添加x
	
	//string& insert(size_t pos, size_t n, char c);
	str.insert(0, 1, 'x');
	//iterator insert(iterator p, char c);
	str.insert(str.begin(), 'x');
	
	cout << str << endl;

	//string& erase(size_t pos = 0, size_t len = npos);
	str.erase(5);//如果len不传值,默认一直删到结尾
	cout << str << endl;

	string s1("hello world");
	s1.replace(5, 3, "%%20");
	//插入3个,就进行平替覆盖	
	cout << s1 << endl;

	string s2("The quick brown fox jumps over a lazy dog");
	string s3;
	for (auto ch : s2)
	{
		if (ch != ' ')
		{
			s3 += ch;
		}
		else
		{
			s3 += "20%";
		}
	}
	//s2 = s3;
	//s2.assign(s3);
	printf("s2: %p\n", s2.c_str());
	printf("s3: %p\n", s3.c_str());

	swap(s2, s3);
	//s2.swap(s3);

	printf("s2: %p\n", s2.c_str());
	printf("s3: %p\n", s3.c_str());

	cout << s2 << endl;
}
void test_string11()
{
	string s1("test.cpp.tar.zip");
	size_t i = s1.rfind('.');

	string s2 = s1.substr(i);
	cout << s2 << endl;

	string s3("https://legeacy.cplusplus.com/reference/string/string/rfind/");
	//协议
	//域名
	//资源名  分割
	string sub1, sub2, sub3;

	//size_t find(const string & str, size_t pos = 0) const;
	size_t i1 = s3.find(':');
	if (i1 != string::npos)//没有找到就会返回string中的npos
		sub1 = s3.substr(0, i1);//从pos位置开始取值,左闭右开
	else
		cout << "没有找到i1" << endl;

	size_t i2 = s3.find('/', i1 + 3);
	if (i2 != string::npos)
		sub2 = s3.substr(i1 + 3, i2 - (i1 + 3));
	else
		cout << "没有找到i2" << endl;

	sub3 = s3.substr(i2 + 1);//默认到npos,即一直到结尾

	cout << sub1 << endl;
	cout << sub2 << endl;
	cout << sub3 << endl;
}
void test_string12()
{
	//查找字符进行替换
	string str("Please, replace the vowels in this sentence by asterisks.");
	size_t found = str.find_first_of("aeiou");
	while (found != string::npos)
	{
		str[found] = '*';
		found = str.find_first_of("aeiou", found + 1);
	}
	cout << str << endl;
}
int main()
{
	//test_string6();
	// 
	//test_string7();
	// 
	//test_stirng8();
	// 
	//test_string9();
	// 
	//test_string10();
	// 
	//test_string11();
	//
	test_string12();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值