C++ find函数简单应用

本文通过示例介绍了如何使用C++标准库中的find函数来查找字符串中的子串,并展示了find函数返回子串的位置索引。此外还演示了find_first_of与find_first_not_of函数的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

find函数可在已知字符串中查找子串

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string s1 = "Rey Dennis Steckler";
	string s2 = "Dennis";
	string s3 = "Ed Wood";

	int f;
	f = s1.find(s2);

	if(f < s1.length())
		cout<<"Found at index: "<<f<<endl;
	else
		cout<<"not found"<<endl;

	f = s1.find(s3);
	if(f < s1.length())
		cout<<"Found at index: "<<endl;
	else
		cout<<"not found"<<endl;

	string s4 = "bby";
	string s5 = "bx";

	cout<<s4.find_first_of(s5)<<endl;
	cout<<s4.find_first_not_of(s5)<<endl;

	return 0;
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值