STL--string容器

1. string的构造函数

string(); 创建一个空字符串,比如string str;
string(const char * s);使用字符串s初始化
string(const string str);使用一个string对象初始化另一个string对象
string(int n, char c);使用n个字符c初始化

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

void test01()
{
	string s1; //默认构造

	const char * str = "asfdg";
	string s2(str);

	string s3(s2);

	string s4(10, 'a');

}

int main()
{
	test01();
	system("pause");
	return 0;
}

2. string赋值

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

void test01()
{
	string str1;
	str1 = "hello";
	cout << str1 << endl;	//hello

	string str2;
	str2 = str1;
	cout << str2 << endl;	//hello

	string str3;
	str3 = 'a';
	cout << str3 << endl;	//a

	string str4;
	str4.assign("helloworld");
	cout << str4 << endl;	//helloworld

	string str5;
	str5.assign("helloworld",5);//取helloworld前5个字符
	cout << str5 << endl;	//hello

	string str6;
	str6.assign(str5);
	cout << str6 << endl;	//hello

	string str7;
	str7.assign(7,'w');//输入7个w
	cout << str7 << endl;	//wwwwwww
}

int main()
{
	test01();
	system("pause");
	return 0;
}

3.string拼接

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

void test01()
{
	string str = "我";
	str += "爱玩游戏";
	cout << str << endl;	//我爱游戏

	str += ':';
	cout << str << endl;	//我爱游戏:

	string str2 = "DNF";
	str += str2;
	cout << str << endl;	//我爱游戏:DNF

	string str3 = "I";
	str3.append(" love ");
	cout << str3 << endl;	//I love 

	str3.append("game:",5);
	cout << str3 << endl;	//I love game:

	//str3.append(str2);
	//cout << str3 << endl;

	string str4 = "LOL DNF";	
	str3.append(str4, 1, 4);//从 第1个位置起,拼接4个
	cout << str3 << endl;//输出:I love game:OL D
}

int main()
{
	test01();
	system("pause");
	return 0;
}

4.string查找和替换

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

void test01()
{
	string str = "abcvdgdg";//从0(下标)开始
//find是从左往右开始找,找到返回其位置下标值,找不到返回-1
	int pos=str.find("dg");	
	cout << "pos = "<<pos<< endl;	//pos=4
//rfind是从右往左开始找
	int pos1 = str.rfind("dg");	
	cout << "pos1 = " << pos1 << endl;	//pos=7
}

void test02()
{
	string str = "asasd";
	str.replace(1,3,"1111");//从1下标位置起,用1111替换3个字符(即sas)
	cout << str << endl;//输出:a1111d
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

5.string字符串比较

string字符串比较(ASCI码比较)
主要用来判断字符串是否相等
字符串A等于B,返回0
字符串A>B,返回1
字符串A<B,返回-1

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

void test01()
{
	string str1 = "xello";
	string str2 = "hello";
	if (str1.compare(str2)==0)
	{
		cout << "str1与str2字符串相等" << endl;
	}
	else if (str1.compare(str2) > 0)
	{
		cout << "str1比str2字符串大" << endl;
	}
	else
	{
		cout << "str1比str2字符串小" << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

6.string存取

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

void test01()
{
	string str = "hello";
	cout << str << endl;
	//1.通过[]访问str单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << " ";
	}
	cout << endl;
	//2.通过at方法访问str单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i)<<" ";
	}
	cout << endl;

	//[]和at方法修改单个字符
	str[0] = 'c';
	cout << str << endl;

	str.at(0) = 'b';
	cout << str << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

7.string插入和删除

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

void test01()
{
	string str = "heiio";
	//插入,在下标个位置1前插入22;
	str.insert(1,"22");
	cout << str << endl;//输出:h22eiio

	//删除,从下标位置2起,删除3个
	str.erase(2, 3);
	cout << str << endl;//输出:h2io

}
int main()
{
	test01();

	system("pause");
	return 0;
}

8.string字串

从字符串里截取我们想要的子串

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

void test01()
{
	string str = "hello world";
	//从下标位置1开始,截取4个字符
	string Substr=str.substr(1,4);
	cout << Substr << endl;//输出:ello
}

void test02()
{
	string str = "zhangsan@qq.com";
	//具体应用:从邮件中获取用户姓名
	int pos = str.find("@");
	string Sub = str.substr(0,pos);
	cout << Sub << endl;
}

int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值