C++ std::string::find_first_not_of 函数详解

在 C++ 标准库中,std::string::find_first_not_of 是一个非常实用的字符串成员函数。它可以帮助我们在字符串中查找第一个属于指定字符集合的字符。在这篇文章中,我们将深入探讨该函数的各种重载语法及其具体用法。

返回值:

如果查找成功,函数返回第一个未匹配字符的索引位置;如果未找到符合条件的字符,则返回 string::npos

语法 1:

查找字符串中第一个不属于 str 参数中指定字符集的字符。

**size_type string::find_first_not_of (const string& str) const**
**str :** 包含用于搜索的字符集的另一个 string 对象。

#### 示例代码 (CPP)

// CPP code for find_first_not_of (const string& str) const

#include 
using namespace std;
  
// Function to demonstrate find_first_not_of
void find_first_not_ofDemo(string str1, string str2)
{
    // Finds first character in str1 which
    // is not present in str2
	string::size_type ch = str1.find_first_not_of(str2);
	cout << "First unmatched character : ";
	cout << str1[ch];
}
         
// Driver code
int main()
{
    string str1("Hello World!");
    string str2("GeeksforGeeks");
 
    cout << "Original String : " << str1 << endl;
    cout << "String to be looked in : " << str2 << endl;
    find_first_not_ofDemo(str1, str2);
  
    return 0;
}

输出结果:

Original String : Hello World!
String to be looked in : GeeksforGeeks
First unmatched character : H

语法 2:

从索引位置 INLINECODE122b0587 开始,查找字符串中第一个不属于 INLINECODEbc1d8be3 参数中指定字符集的字符。

**size_type string::find_first_not_of (const string& str, size_type idx) const**
**str :** 包含用于搜索的字符集的另一个 string 对象。
**idx :** 开始搜索的起始索引位置。

#### 示例代码 (CPP)

// CPP code for string::find_first_not_of
// (const string& str, size_type idx) const

#include 
using namespace std;
  
// Function to demonstrate find_first_not_of
void find_first_not_ofDemo(string str1, string str2)
{
    // Finds first character in str1 from index 3 which
    // is not present in str2
	string::size_type ch = str1.find_first_not_of(str2, 3);
	cout << "First unmatched character : ";
	cout << str1[ch];
}
         
// Driver code
int main()
{
    string str1("geeKsforgeeks");
    string str2("GeeksforGeeks");
 
    cout << "Original String : " << str1 << endl;
    cout << "String to be looked in : " << str2 << endl;
    find_first_not_ofDemo(str1, str2);
  
    return 0;
}

输出结果:

Original String : geeKsforgeeks
String to be looked in : GeeksforGeeks
First unmatched character : K

语法 3:

查找字符串中第一个不属于 C 字符串 cstr 中指定字符集的字符。

**size_type string::find_first_not_of (const char* cstr) const**
**cstr :** 包含用于搜索的字符集的 C 风格字符串。

注意: cstr 不能是空指针(NULL)。

#### 示例代码 (CPP)

// CPP code for string::find_first_not_of (const char* cstr) const

#include 
using namespace std;
  
// Function to demonstrate find_first_not_of
void find_first_not_ofDemo(string str)
{
    // Finds first character in str which
    // is not present in "geeksforgeeks"
	string::size_type ch = str.find_first_not_of("geeksforgeeks");
	cout << "First unmatched character : ";
	cout << str[ch];
}
         
// Driver code
int main()
{
    string str("GeeksforGeeks");
 
    cout << "Original String : " << str << endl;
    find_first_not_ofDemo(str);
  
    return 0;
}

输出结果:

Original String : GeeksforGeeks
First unmatched character : G

语法 4:

从索引位置 INLINECODE354574ad 开始,查找字符串中第一个不属于 C 字符串 INLINECODE255cca98 中指定字符集的字符。

**size_type string:: find_first_not_of (const char* cstr, size_type idx) const**
**cstr :** 包含用于搜索的字符集的 C 风格字符串。
**idx :** 开始搜索的起始索引位置。

注意: cstr 不能是空指针(NULL)。

#### 示例代码 (CPP)

// CPP code for size_type string:: find_first_not_of 
// (const char* cstr, size_type idx) const

#include 
using namespace std;
  
// Function to demonstrate find_first_not_of
void find_first_not_ofDemo(string str)
{
    // Finds first character in str from 5th index which
    // is not present in "geeksforgeeks"
	string::size_type ch = str.find_first_not_of("geeksForgeeks", 5);
	cout << "First unmatched character : ";
	cout << str[ch];
}
         
// Driver code
int main()
{
    string str("GeeksforGeeks");
 
    cout << "Original String : " << str << endl;
    find_first_not_ofDemo(str);
  
    return 0;
}

输出结果:

Original String : GeeksforGeeks
First unmatched character : f

语法 5:

查找字符串中第一个不等于字符 c 的字符。

**size_type string::find_first_not_of (char c) const**
**c :** 用于与字符串内容进行比较的单个字符。

(注:原文档最后部分代码未显示完整,以上内容涵盖了该函数的核心用法)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。如需转载,请注明文章出处豆丁博客和来源网址。https://shluqu.cn/43500.html
点赞
0.00 平均评分 (0% 分数) - 0