C++ STL进阶与补充(string容器)

String是C++字符串,本质上是一个类,类内部封装了char*,因此它是一个char*型的容器,其内部封装了很多成员方法,例如find,copy,delete,insert等。String管理char*所分配的内存,不用担心复制越界或者取值越界,由类的内部进行负责。

1、构造函数

    String(); //创建一个空的字符串

    String(const char* s); //使用字符串s进行初始化

    String(const string& str); //使用一个string对象初始化另一个string对象

    String(int n, char c); //使用n个字符c初始化

2、string赋值操作

    一般来说,一般使用“=”的方式进行赋值。

//第一种等号方式赋值
	string str1;  
	str1 = "Hello world!";
	cout << "str1=" << str1 << endl;
	//第二种等号方式赋值
	string str2;
	str2 = str1;
	cout << "str2=" << str2 << endl;
	//第一种assign函数实现
	string str4;
	str4.assign("Hello C++!");
	cout << "str4=" << str4<< endl;
	//第二种assign函数实现,实现只赋值前几个字符
	string str5;
	str5.assign("Hello C++!",5);
	cout << "str5=" << str5<< endl;
	//第三种assign函数,将一个字符串赋值给另一个字符串
	string str6;
	str6.assign(str5);
	cout << "str6=" << str6 << endl;

3、string拼接操作

功能:实现在字符串的末尾拼接字符串。

//使用运算符+=
	string str1 = "我";
	str1 += "爱玩游戏";
	str1 += ':';
	cout << "str1=" << str1 << endl;
	string str2 = "LOL DNF";
	str1 += str2;
	cout << "str1=" << str1 << endl;
	//使用append函数
	string str3 = "I";
	str3.append(" love ");
	cout << "str3=" << str3 << endl;
	//append只拼接n个字符
	//str3.append("game abcde", 4);   //拼接前四个字符
	str3.append("game abcde", 0,4);  //拼接前四个字符,从第0个位置开始,截取4个字符
	cout << "str3=" << str3 << endl;

4、string查找与替换

查找:查找指定字符串是否存在;

替换:在指定位置替换字符串;

#include<iostream>
#include"string"
using namespace std;
//1、查找
void test01()
{
	string str1 = "abcdefgde";
	int pos1=str1.find("de");
	cout << "pos="<<pos1 << endl; //输出3,因为索引从0开始
	int pos2 = str1.find("df");
	cout << "pos=" << pos2 << endl; //找不到则返回-1

	//find和rfind的区别
	//rfind从右往左查找,find从左往右查找
	int pos3 = str1.rfind("de");
	cout << "pos=" << pos3 << endl;  //输出7
}
//2、替换
void test02()
{
	string str2 = "abcdefg";
	str2.replace(1, 3, "1111"); //一共替换了4个1进去,变成a1111efg,说明bcd替换成了1111
	cout << "str2=" << str2 << endl;
}
int main()
{
	//test01();
	test02();
}

5、字符串比较

功能:比较字符串的ASCII码,逐个挨个进行对比,如果两者相等则返0,如果前者大返回1,如果后者大返回-1。

Compare最大的用途就是比两者是否相等。但对比谁大谁小意义不大。

#include<iostream>
#include"string"
using namespace std;
void test01()
{
	string str1 = "xello";
	string str2 = "yello";
	if (str1.compare(str2) == 0)
	{
		cout << "两者相等" << endl;
	}
	else if (str1.compare(str2) > 0)
	{
		cout << "str1>str2" << endl;
	}
	else
	{
		cout << "str1<str2" << endl;
	}
}
int main()
{
	test01();
}

6、string字符存取

作用:提取字符串中的具体字符。通常可以利用字符数组的下标来提取。也可以利用函数。

#include<iostream>
#include"string"
using namespace std;
void test01()
{
	string str1 = "hello";
	for (int i = 0; i < str1.size(); i++)
	{
		cout << str1[i] << " ";
	}
	cout << endl;
	//修改字符
	str1[0] = 'H';
	cout << str1 << endl;
}
int main()
{
	test01();
}

7、string字符串插入和删除

String字符串通过Insert函数完成插入操作,通过erase删除指定位置的字符。

需要注意的是,下标都是从0开始算的。

#include<iostream>
#include"string"
using namespace std;
void test01()
{
	string str = "Hello!";
	//插入
	str.insert(1, "111");//从第一个位置插入字符串
	cout << "str=" << str << endl; //输出H111ello
	//删除
	str.erase(1, 3);//从第1个位置起,删除3个字符
	cout << "str=" << str << endl; //输出Hello
}
int main()
{
	test01();
}

8、子串获取

从一段字符串中截取一段我们想要的字符串。需要明确开始截断的位置和持续的字符数。

#include<iostream>
#include"string"
using namespace std;
void test01()
{
	string str = "Hello!";
	string subStr = str.substr(1, 3);
	cout << "subStr=" << subStr << endl;
}
void test02()
{
	string email = "zhangsan@sina.com";
	//从邮件地址中获取用户名信息
	//先找@的位置
	int pos = email.find("@");  //位置在8号
	string name = email.substr(0, pos); //从0开始,截8个位置就可以了
	cout << "name is " << name << endl;
}
int main()
{
	//test01();
	test02();
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值