进程 中的 int find_first_of(string&

首页 / C++入门教程 / 进程 中的 int find_first_of(string&

此函数用于查找指定字符首次出现的位置。

find_first_of - 语法

str1.find_first_of(str);

find_first_of - 参数

str       - 包含要搜索的字符的字符串。

pos       - 它定义开始搜索的位置。

n            -  标识要搜索的字符的字符数。

ch          -  它定义了要搜索的字符

find_first_of - 返回值

它返回搜索字符的位置。

find_first_of - 例子1

让我们看一个简单的例子。

#include<iostream>
using namespace std;
int main()
{
        string str1 = "Dancing is my favorite hobby";
        cout << "String contains :"<< str1<< '\n';
        cout <<"Position of the first occurrence of the string 'favorite' is " <<                      str1.find_first_of("favorite");
         return 0;         
}  

输出:

String contains : Dancing is my favorite hobby
Position of the first occurrence of the string favorite is 1

find_first_of - 例子2

让我们看一个简单的示例,其中指定了开始搜索的位置。

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-string-find-first-of-function.html

来源:LearnFk无涯教程网

#include<iostream>
using namespace std;
int main()
{
string str = "Welcome to the programming world";
cout<< "String contains : "<< str <<'\n';
 cout<<"Now, start the search from the second position and we find the first occurrence of the 'programming' is :"<<str.find_first_of("programming",2);
return 0;
}

输出:

String contains : Welcome to the programming world
Now, start the search from the second position and we find the first occurrence of the 'programming' is : 4

find_first_of - 例子3

让我们看一个简单的示例,查找单个字符首次出现的位置。

#include<iostream>
using namespace std;
int main()
{
string str = "learnfk tutorial";
cout << "String contains :" << str<< '\n';
cout <<"Position of the first occurrence of 'a' character is :" << str.find_first_of('a');
return 0;
} 

输出:

String contains learnfk tutorial
Position of the first occurrence of 'a' character is : 1

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

赵成的运维体系管理课 -〔赵成〕

硅谷产品实战36讲 -〔曲晓音〕

深入拆解Tomcat & Jetty -〔李号双〕

微信小程序全栈开发实战 -〔李艺〕

To B市场品牌实战课 -〔曹林〕

分布式金融架构课 -〔任杰〕

容量保障核心技术与实战 -〔吴骏龙〕

反爬虫兵法演绎20讲 -〔DS Hunter〕

手把手带你写一个 MiniTomcat -〔郭屹〕

好记忆不如烂笔头。留下您的足迹吧 :)