C++第十七弹---string使用(下)

个人主页: 熬夜学编程的小林

💗系列专栏: 【C语言详解】 【数据结构详解】【C++详解】

目录

1、标准库中的string类

1.1、string类的常用接口说明

 1.1.1、string类对象的修改操作 

 1.1.2、string类对象非成员函数重载

总结


1、标准库中的string类

1.1、string类的常用接口说明

 1.1.1、string类对象的修改操作 

    1、 push_back(char c) //尾插一个字符
    2、 string& append(const string & str);//在字符串尾追加一个string类字符串
    3、 string& append(const char* s);//在字符串尾追加一个常量字符串
    4、 string& operator+= (const string & str);//在字符串尾追加一个string类字符串
    5、 string& operator+= (const char* s);//在字符串尾追加一个常量字符串
    6、 string& assign(const string & str);//把原来string类字符串替换成str类
    7、 string& assign(const char* s);//把原来string类字符串替换成常量字符串
    8、 string& insert(size_t pos, const string & str);//在pos位置插入类字符串
    9、 string& insert(size_t pos, const char* s);//在pos位置插入常量字符串
    10、string& erase (size_t pos = 0, size_t len = npos);//从pos位置删除len长度个字符,超过类大小则删除所有有效字符
    11、string& replace (size_t pos,  size_t len,  const string& str);//将原类的pos位置向后len个长度替换成str类
    12、string& replace (size_t pos,  size_t len,  const char* s);//将原类的pos位置向后len个长度替换成常量字符串
    13、void swap(string & str);//将原类与str类交换数据
    14、void pop_back();//尾删一个字符

注意:关于字符串修改的重载函数实在太多了,因此只举例了常见的函数进行使用。

int main()
{
	string s1;
	string s2("hello");
	s1.push_back('a');//在s1尾部插入字符a
	s1.push_back('b');//在s1尾部插入字符b
	s1.push_back('c');//在s1尾部插入字符c
	cout << s1 << endl;//打印s1
	s1.append(s2);//在s1尾部追加string类s2
	cout << s1 << endl;
	s1.append("world");//在s1尾部追加world字符串
	cout << s1 << endl;
	s1 += s2;//在s1尾部追加string类s2
	cout << s1 << endl;
	s1 += "world";//在s1尾部追加world字符串
	cout << s1 << endl;
	s1.assign(s2);//将s1原来的字符替换成s2
	cout << s1 << endl;
	s1.assign("world");//将s1原来的字符替换成world
	cout << s1 << endl;
	s1.insert(2, s2);//在2的位置插入string类s2
	cout << s1 << endl;
	s1.insert(3, "world");//在3的位置插入字符串world
	cout << s1 << endl;
	s1.erase(2, 3);//从2的位置删除3个字符
	cout << s1 << endl;
	s1.replace(0, 4, s2);//从0的位置开始把4个位置替换成s2
	cout << s1 << endl;
	s1.replace(2, 3, "world");//从2的位置开始把3个位置替换world
	cout << s1 << endl;
	cout << s2 << endl;
	s1.swap(s2);//将s1与s2数据交换
	cout << s1 << endl;
	cout << s2 << endl;
	s1.pop_back();//尾删一个字符
	cout << s1 << endl;
	return 0;
}

1.1.1、string类对象的字符串操作 

    1、 const char* c_str() const;//获取C格式常量字符串
    2、 size_t find (const string& str, size_t pos = 0) const;//从pos位置开始从前往后找与str相等的字符串,相等则返回该串第一个元素下标,否则返回npos
    3、 size_t find(const char* s, size_t pos = 0) const;//从pos位置开始从前往后找与s相等的字符串,相等则返回该串第一个元素下标,否则返回npos
    4、 size_t rfind (const string& str, size_t pos = npos) const;//从pos位置开始从后往前找与str相等的字符串,相等则返回该串第一个元素下标,否则返回npos
    5、 size_t rfind(const char* s, size_t pos = npos) const;//从pos位置开始从后往前找与s相等的字符串,相等则返回该串第一个元素下标,否则返回npos
    6、 size_t find_first_of(const string & str, size_t pos = 0) const;//从pos位置开始从前往后找与str其中一个字符相等的元素,找到则返回此元素下标,否则返回npos
    7、 size_t find_first_of(const char* s, size_t pos = 0) const;//从pos位置开始从前往后找与s其中一个字符相等的元素,找到则返回此元素元素下标,否则返回npos
    8、 size_t find_last_of(const string & str, size_t pos = npos) const;//从pos位置开始从后往前找与str其中一个字符相等的元素,找到则返回此元素下标,否则返回npos
    9、 size_t find_last_of(const char* s, size_t pos = npos) const;//从pos位置开始从后往前找与s其中一个字符相等的元素,找到则返回此元素下标,否则返回npos
    10、size_t find_first_not_of (const string& str, size_t pos = 0) const;//从pos位置开始从前往后找与str中字符都不相等的元素,找到则返回此元素下标,否则返回npos
    11、size_t find_first_not_of(const char* s, size_t pos = 0) const;//从pos位置开始从前往后找与s中字符都不相等的元素,找到则返回此元素下标,否则返回npos
    12、size_t find_last_not_of (const string& str, size_t pos = npos) const;//从pos位置开始从后往前找与str中字符都不相等的元素,找到则返回此元素下标,否则返回npos
    13、size_t find_last_not_of(const char* s, size_t pos = npos) const;//从pos位置开始从后往前找与s中字符都不相等的元素,找到则返回此元素下标,否则返回npos
    14、string substr (size_t pos = 0, size_t len = npos) const;//从pos位置截取len长度的子串

int main()
{
	string s("hello world");
	const char* str = s.c_str();//将C格式字符串赋值给str
	cout << str << endl;//打印str字符串
	string s1(":");
	string s2;
	const char* str1 = ":";
	string url("https://cplusplus.com/reference/string/string/");
	size_t pos1 = url.find(s1, 0);//从0位置开始从前往后找与s1相等的字符串
	cout << pos1 << endl;
	s2 = url.substr(0, pos1);//从0位置截取pos1长度的子串
	cout << s2 << endl;//打印上面子串

	size_t pos2 = url.find(str1, 0);//从0位置开始从前往后找与str1相等的字符串
	cout << pos2 << endl;
	s2 = url.substr(0, pos2);//从0位置截取pos2长度的子串
	cout << s2 << endl;//打印上面子串

	string s3 = "code.c.dir.exe";
	size_t pos3 = s3.rfind(".");//从npos位置开始从后往前找与.相等的字符
	cout << pos3 << endl;
	s2 = s3.substr(pos3);//从pos位置截取字符串
	cout << s2 << endl;//打印上面子串

	string s4 = "abcdef";
	cout << s4 << endl;//打印初始s4
	//将aeiou的字符均填充成*
	size_t pos4 = s4.find_first_of("aeiou", 0);//从0位置开始从前往后找与"aeiou"其中一个字符相等的元素
	while (pos4 != string::npos)
	{
		s4[pos4] = '*';
		pos4 = s4.find_first_of("aeiou", pos4 + 1);
	}
	cout << s4 << endl;//打印填充后的s4
	return 0;
}

上述代码输出结果: 

注意:标准库中的npos源代码为:static const size_t npos = -1;

 1.1.2、string类对象非成员函数重载

    1、string operator+ (const string& lhs, const string& rhs);//在lhs尾部追加rhs字符串
    2、string operator+ (const char*   lhs, const string& rhs);//在rhs尾部追加lhs字符串
    3、bool operator== (const string& lhs, const string& rhs);//比较lhs与rhs字符串,逐个字符比较
    4、bool operator== (const char*   lhs, const string& rhs);//比较lhs与rhs字符串,逐个字符比较
    5、void swap (string& x, string& y);//将x字符串与y字符串交换,此处思想为交换指针
    6、istream& operator>> (istream& is, string& str);//输入运算符重载
    7、ostream& operator<< (ostream& os, const string& str);//输出运算符重载
    8、istream& getline(istream & is, string & str);//获取一行字符串

int main()
{
	string s1("hello");
	const char* str = "world";
	string s2;
	s2 = s1 + str;//类字符串+C常量字符串,传值返回尽量少用
	cout << s2 << endl;//输出运算符重载
	s2 = str + s1;//C常量字符串+类字符串
	cout << s2 << endl;

	string s3 = "hello";
	const char* str3 = "hello";
	cout << (s3 == str3) << endl;//左边类字符串 右边C格式字符串
	cout << (str3 == s3) << endl;
	cout << (s3 < str3) << endl;

	string s4 = "world";
	cout << s3 << endl;
	cout << s4 << endl;
	swap(s3, s4);//交换s3与s4,C++标准库有swap函数,此处还有不会出错?根据模板的规则,有现成的用现成的,因此没问题。

	string s5;
	//cin >> s5;//输入字符串给s5
	//cout << s5 << endl;

	getline(cin, s5);//获取一行字符串
	cout << s5 << endl;
	return 0;
}

 

总结


本篇博客就结束啦,谢谢大家的观看,如果公主少年们有好的建议可以留言喔,谢谢大家啦!

  • 167
    点赞
  • 136
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 136
    评论
评论 136
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小林熬夜学编程

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

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

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

打赏作者

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

抵扣说明:

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

余额充值