std::find_if_std :: find_first_of()与C ++中的示例

std::find_if

std :: find_first_of()作为STL函数 (std::find_first_of() as a STL function)

find_first_of() in a useful STL function, which is  invoked by a string and searches the first position of any character out of the character list provided(string str) in the argument.

find_first_of()在有用的STL函数中,该函数由字符串调用,并从参数中提供的字符列表(字符串str)中搜索任何字符的第一位置。

Syntax:

句法:

size_t find_first_of (const string& str, size_t pos = 0) const;

Where,

哪里,

  • const string& str - collection of characters any of which needs to be searched.

    const string&str-需要搜索任何字符的集合。

  • size_t pos - an optional parameter which is passed with a default value of 0. Position defines the start index of the invoking string from where searching should start.

    size_t pos-一个可选参数,默认值为0传递。position定义了从开始搜索的位置开始的调用字符串的起始索引。

Return type: size_t

返回类型: size_t

It returns the index if it finds any character of the mentioned collection in the invoking string. If none of the characters is present in the invoking string then it returns a string::npos.

如果在调用字符串中找到所提及集合的任何字符,则返回索引。 如果调用字符串中没有字符,则返回string :: npos

It returns the index immediately if any of the characters found.

如果找到任何字符,它将立即返回索引。

Example 1: Position argument not passed, so by default it's 0

示例1:未传递位置参数,因此默认为0

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str = "includehelp";

    string re = "aeiou";
    //its searching in the string str
    //it searches any character from the string re
    //now if any character of re is found then it simply 
    //returns the position of the character found

    cout << "the first vowel appearing in the string: " << str << " is " << str[str.find_first_of(re)] << endl;

    return 0;
}

Output:

输出:

the first vowel appearing in the string: includehelp is i

In the above program, 
In the first case,
The invoking string is str, "includehelp"
The collection of characters which are being searched 
"aeiou" -> 'a', 'e', 'i', 'o', 'u'

The position argument is not passed so 
it will start searching from position 0 which is 'i', 
since 'i' falls in the collection of characters thus it returns 0

Example 2: Position argument is passed

示例2:传递位置参数

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str = "includehelp";

    string re = "aeiou";
    //its searching in the string str
    //it searches any character from the string re
    //now if any character of re is found then it simply 
    //returns the position of the character found

    //now this will start searching in str from position=1
    //thus it skips the fisrt vowel 'i'
    cout << "the second vowel appearing in the string: " << str << " is " << str[str.find_first_of(re, 1)] << endl;

    return 0;
}

Output:

输出:

the second vowel appearing in the string: includehelp is u

In the above case, the starting position is 1 thus it starts searching only from index 1 skipping the first index(0) which is 'i'. Thus, the first character out of the collection which is found is 'u'.

在上述情况下,起始位置为1,因此它仅从索引1开始搜索,而跳过了第一索引(0) 'i' 。 因此,从集合中找到的第一个字符是'u'

翻译自: https://www.includehelp.com/stl/std-find_first_of-with-examples-in-cpp.aspx

std::find_if

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值