C++:STL之String容器

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

在这里插入图片描述

//string的构造函数,在string类型中中文占两个位置
void test01()
{
	//默认构造string();
	string s;
	cout << s << endl;

	//string(const char * s);字符初始化
	const char * str = "Hello Word";
	string s1(str);
	cout << s1 << endl;

	//string(const string& s);用str初始化另外一个str
	string s2(s1);
	cout << s2 << endl;

	//string(int num,char a);用num个a初始化string
	string s3(10, 'a');
	cout << s3 << endl;
}

在这里插入图片描述

//string的赋值操作
void test02()
{
	string str1;
	str1 = "Hello Word";				//const char*类型赋值给当前字符串
	cout << "str1 = " << str1 << endl;

	string str2;
	str2 = str1;						//把字符串str1赋值给当前字符串
	cout << "str2 = " << str2 << endl;

	string str3;
	str3 = 'a';							//把字符赋值给当前字符串
	cout << "str3 = " << str3 << endl;

	string str4;
	str4.assign("Hello C++");			//const char*类型赋值给当前字符串
	cout << "str4 = " << str4 << endl;

	string str5;
	str5.assign("Hello C++",5);			//将const char*类型的字符串前5个字符串赋值给当前字符串
	cout << "str4 = " << str4 << endl;

	string str6;
	str6.assign(str5);					//把字符串str5赋值给当前字符串
	cout << "str6 = " << str6 << endl;

	string str7;
	str7.assign(10,'a');				//用n个字符赋值当前字符串
	cout << "str7 = " << str7 << endl;
}

在这里插入图片描述

//字符串拼接
void test03()
{
	string str1="我";
	str1 += "爱玩";						//重载+=运算符const char*类型
	cout << "str1 = " << str1 << endl;

	str1 += ":";						//重载+=运算符char类型
	cout << "str1 = " << str1 << endl;	

	string str3 = "英雄联盟";
	str1 += str3;						//重载-=运算符string类型
	cout << "str1 = " << str1 << endl;	

	str1.append("、PUBG");				//调用append函数const char*类型
	cout << "str1 = " << str1 << endl;

	str1.append("xxx", 2);				//把字符串前n个字符连接到末尾
	cout << "str1 = " << str1 << endl;

	string str4="喵喵喵";
	str1.append(str4);					//调用append函数连接另外一个字符串
	cout << "str1 = " << str1 << endl;

	string str5 = "abcdefg";
	str1.append(str5, 3, 2);			//调用append函数,把str5的下标3后的两个字符连接到末尾
	cout << "str1 = " << str1 << endl;
}

在这里插入图片描述

//字符串查找和替换
void test04()
{
	string str1("abcdefghijklmn");

	//int find(const string& str,int pos = 0)const;
	int pos = str1.find("af");					//查找str第一次出现的位置,从pos开始查找,找到则返回第一个字符下标,未找到则返回-1
	cout << "下标位置为:" << pos << endl;
	//int rfind(const string& str,int pos = npos)const;
	pos = str1.rfind("mn");					//查找str最后一次出现的位置,从pos开始查找,找到则返回第一个字符下标,未找到则返回-1
	cout << "下标位置为:" << pos << endl;
	//string& replace(int pos,int n,const string& str);
	str1.replace(1, 3, "卧槽");			//把从下标pos到n位置替换成str
	cout << str1 << endl;

}

在这里插入图片描述

//字符串比较
void test05()
{
	string str1("Hello");
	string str2("Hello");
	cout << "比较结果为:" << str1.compare(str2) << endl;//=返回0,>返回1,<返回-1
	//也可以用关系运算符直接做比较
	cout << (str1 == str2) << endl;
	
}

在这里插入图片描述

//字符存取
void test06()
{
	string str("Hello");
	cout << str[1] << endl;		//重载[]方式
	cout << str.at(2) << endl;	//调用at
}

在这里插入图片描述

//插入删除
void test07()
{
	//string& insert(int pos, const char* s);
	string str("he");
	str.insert(1, "a");		//在pos位置插入字符串
	cout << str << endl;

	//string& insert(int pos,int n, char c);
	str.insert(3, 4, 97);	//在pos位置插入n个c
	cout << str << endl;

	//string& erse(int pos, int n = npos);
	str.erase(3, 2);		//删除pos位置开始的n个字符;
	cout << str << endl;
}

在这里插入图片描述

//子串
void test08()
{
	//string substr(int pos = 0, int n = npos)const;
	string str("Hello Word");
	string str1 = str.substr(0, 5);	//返回由pos开始的n个字符串组成的字符串
	cout << str1 << endl;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值