浅析 c++ string 使用方法

目录

@[TOC](string 构造函数初始化 )

@[TOC](string输入)

@[TOC](使用字符串)

 

#string 构造函数初始化

string类封装在string头文件里,里面封装了常用的方法,包括比较字符串、遍历、构造函数、查找以及各种访问元素的重载函数

string构造函数
string(const char* s)将指针s(以空字符为结尾的字符串)中内容赋值给string对象
string(size_type n,c)将包含n个元素的string对象,都赋为字符c
string(const string& str)将string 对象赋值为str(复制构造函数)
string(const char* s,size_type n)将指针s中前n个字符赋值给string对象

string operator+=(const string&str_1,const string& str_2);=

将str_1和str_2连接后赋值给string对象

 

 

 

 

 

 

 

 

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	const char* s = "string";
	string str_1(s);//将指针s内容复制到string对象中
	cout << str_1 << endl;
	string str_2(5, 'c');//将string对象前五个字符都赋值为字符c
	cout << str_2 << endl;
	string str_3(str_1);//将str_1内容复制到str_3中
	cout << str_3 << endl;
	string str_4(s, 3);//将str_4内容赋值为指针s前三个字符
	cout << str_4 << endl;
	string str_5;
	str_5 = str_1 + str_2;//连接字符串
	cout << str_5;
	return 0;
}

代码结果:

#string 输入

对于常用的字符串,我们可能常会使用字符数组,但是字符数组需要指定长度

char str[50];

getline(cin,50);

对于字符我们往往不知道输入长度string对象可以自动调整string对象大小,使之刚好能够存储字符串。

string str;

cin>>str;

或getline(cin,str);

#使用字符串

##比较字符串

string对象还可以比较字符串,String 类对六种关系运算符都进行了重载。如果机器排列顺序中,一个对象位于另一个对象前面,则认为前者小于后者。

如果机器排列序列为ASCII码,根据ASCII码比较对象大小。string对象能与另一个string对象、C-风格字符串进行比较,C-风格字符串与string对象比较

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	string str_1("cobra");
	string str_2("coral");
	const char str_3[10] = "cool";
	if (str_1 < str_2)
	{
		cout << "<" << endl;
	}
	if (str_1 == str_3)
	{
		cout << "=" << endl;
	}
	if (str_3 != str_1)
	{
		cout << "!=";
	}
	return 0;
}

代码结果:

##确定字符串长度

size()和length()成员函数都返回字符串字符数

##搜索给定字符串

重载的find()方法
方法原型描述
size_type find(const string& str,size_type pos=0)const从字符串的pos开始查找子字符串str,如果找到则返回子字符串首次出现的字符索引;否则,返回string::npos
size_type find(const char* s,size_type pos=0)const从字符串pos开始查找子字符串s,如果找到,返回字符串首次出现的位置的索引,否则返回string::npos

size_type find(const

char* s,size_type pos,size_type n)

从字符串pos位置开始查找s的前n个字符串,如果找到则返回其首次出现的位置的索引,否则返回string::npos

size_type find(char ch,

size_type pos=0)

从字符串pos开始查找字符ch,如果找到,返回字符首次出现的位置,否则,返回string::npos

 

 

 

 

 

 

 

 

 

 

下面我们来看看例子:

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	string str_1= "string string string!";
	string str_2("str");
	int place_first = str_1.find("ing");//size_type find(const char* s,size_type pos=0)
	cout << "first place of ing: " << place_first << endl;
	int place_second = str_1.find(str_2);//size_type find(const string& str,size_type pos=0)
	cout << "first place of str: " << place_second << endl;
	cout << str_1.find("ring", 0, 2) << endl;//查找str_1中,ring前两个字符,起始位置为str_1初始位置
	cout << str_1.find('r');//查找str_1中字符r首次出现的位置,起始位置默认为str_1初始位置
	return 0;
}

运行结果:

、、

string 库还提供了相关的其他方法:rfind()、find_first_of()、find_last_of()、find_first_not_of()、find_not_last_of()。他们的用法和find()相同。rfind()方法查找字符串或字符最后一次出现的位置。

find_first_of()方法查找字符串中任意一个字符首次出现的位置.例如:

string str("string");

int where =str.find_first_of("hark");

该语句返回字符r在"string"中首次出现的位置。find_last_of()用法与此相同,只是查找最后一次r出现的位置。

##删除指定字符串

想要删除指定字符串,可以先用find()方法查找字符或字符串的位置,再用erase()函数删除

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	string str_1= "string!";
	int where = str_1.find("tr");//查找字符串tr出现的首个位置
	str_1.erase(where,2);//从查找的首个位置开始,删除2个字符
	cout << str_1;
	return 0;
}

执行结果:sing!

string库提供了很多其他的方法来处理字符串我就不一一以介绍,对于可能出现的错误请指出。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值