📜  C++中的std :: 字符串:: find_last_not_of

📅  最后修改于: 2021-05-30 05:17:56             🧑  作者: Mango

它搜索的第一个字符的字符串,从字符串的结尾,这不符合任何在它的参数中指定的字符。
返回值:成功时第一个不匹配字符的索引;如果找不到这样的字符,则返回字符串:: npos。
语法1:搜索不是字符串str元素的最后一个字符。

size_type string::find_last_not_of (const string& str) const
str : Another string with the set of characters
to be used in the search.
CPP
// CPP code for find_last_not_of (const string& str) const
 
#include 
using namespace std;
 
// Function to demonstrate find_last_not_of
void find_last_not_ofDemo(string str1, string str2)
{
    // Finds last character in str1 which
    // is not present in str2
    string::size_type ch = str1.find_last_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_last_not_ofDemo(str1, str2);
 
    return 0;
}


CPP
// CPP code for string::find_last_not_of
// (const string& str, size_type idx) const
 
#include 
using namespace std;
 
// Function to demonstrate find_last_not_of
void find_last_not_ofDemo(string str1, string str2)
{
    // Finds last character in str1 from index 3 which
    // is not present in str2
    string::size_type ch = str1.find_last_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_last_not_ofDemo(str1, str2);
 
    return 0;
}


CPP
// CPP code for string::find_last_not_of (const char* cstr) const
 
#include 
using namespace std;
 
// Function to demonstrate find_last_not_of
void find_last_not_ofDemo(string str)
{
    // Finds last character in str which
    // is not present in "geeksforgeeks"
    string::size_type ch = str.find_last_not_of("geeksforgeeks");
    cout << "First unmatched character : ";
    cout << str[ch];
}
 
// Driver code
int main()
{
    string str("GeeksforGeeks");
 
    cout << "Original String : " << str << endl;
    find_last_not_ofDemo(str);
 
    return 0;
}


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


CPP
// CPP code for size_type string:: find_last_not_of (char c) const
 
#include 
using namespace std;
 
// Function to demonstrate find_last_not_of
void find_last_not_ofDemo(string str)
{
    // Finds last character in str which
    // is not equal to character 'G'
    string::size_type ch = str.find_last_not_of('G');
    cout << "First unmatched character : ";
    cout << str[ch];
}
 
// Driver code
int main()
{
    string str("GeeksforGeeks");
 
    cout << "Original String : " << str << endl;
    find_last_not_ofDemo(str);
 
    return 0;
}


CPP
// CPP code for size_type string::find_last_not_of
// (char c, size_type idx) const
 
#include 
using namespace std;
 
// Function to demonstrate find_last_not_of
void find_last_not_ofDemo(string str)
{
    // Finds last character in str from 6th index which
    // is not equal to character 'G'
    string::size_type ch = str.find_last_not_of('G', 6);
    cout << "First unmatched character : ";
    cout << str[ch];
}
 
// Driver code
int main()
{
    string str("GeeksforGeeks");
 
    cout << "Original String : " << str << endl;
    find_last_not_ofDemo(str);
 
    return 0;
}


CPP
// CPP code for size_type string::find_last_not_of
// (const char* chars, size_type idx, size_type chars_len) const
 
#include 
using namespace std;
 
// Function to demonstrate find_first_not_of
void find_last_not_ofDemo(string str)
{
    // Finds last character in str from 4th index which
    // is not equal to any of the first 3 characters from "svmnist"
    string::size_type ch = str.find_last_not_of("svmnist", 4, 3);
    cout << "First unmatched character : ";
    cout << str[ch];
}
 
// Driver code
int main()
{
    string str("GeeksforGeeks");
 
    cout << "Original String : " << str << endl;
    find_last_not_ofDemo(str);
 
    return 0;
}


输出:

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

语法2:从索引idx搜索不是字符串str元素的最后一个字符。

size_type string::find_last_not_of (const string& str, size_type idx) const
str : Another string with the set of characters
to be used in the search.
idx : is the index number from where we have to
start finding first unmatched character.

CPP

// CPP code for string::find_last_not_of
// (const string& str, size_type idx) const
 
#include 
using namespace std;
 
// Function to demonstrate find_last_not_of
void find_last_not_ofDemo(string str1, string str2)
{
    // Finds last character in str1 from index 3 which
    // is not present in str2
    string::size_type ch = str1.find_last_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_last_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_last_not_of (const char* cstr) const
cstr : Another C-string with the set of characters
to be used in the search.

请注意,cstr可能不是空指针(NULL)。

CPP

// CPP code for string::find_last_not_of (const char* cstr) const
 
#include 
using namespace std;
 
// Function to demonstrate find_last_not_of
void find_last_not_ofDemo(string str)
{
    // Finds last character in str which
    // is not present in "geeksforgeeks"
    string::size_type ch = str.find_last_not_of("geeksforgeeks");
    cout << "First unmatched character : ";
    cout << str[ch];
}
 
// Driver code
int main()
{
    string str("GeeksforGeeks");
 
    cout << "Original String : " << str << endl;
    find_last_not_ofDemo(str);
 
    return 0;
}

输出:

Original String : GeeksforGeeks
First unmatched character : G

语法4:从索引idx搜索不是C字符串cstr元素的最后一个字符

size_type string::find_last_not_of (const string& str, size_type idx) const
cstr : Another C-string with the set of characters
to be used in the search.
idx : is the index number from where we have to
start finding first unmatched character.

请注意,cstr可能不是空指针(NULL)。

CPP

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

输出:

Original String : GeeksforGeeks
First unmatched character : f

语法5:查找str中不等于char c的最后一个字符。

size_type string::find_last_not_of (const char* cstr) const
c: Character c with which contents of str are required to be compared.

CPP

// CPP code for size_type string:: find_last_not_of (char c) const
 
#include 
using namespace std;
 
// Function to demonstrate find_last_not_of
void find_last_not_ofDemo(string str)
{
    // Finds last character in str which
    // is not equal to character 'G'
    string::size_type ch = str.find_last_not_of('G');
    cout << "First unmatched character : ";
    cout << str[ch];
}
 
// Driver code
int main()
{
    string str("GeeksforGeeks");
 
    cout << "Original String : " << str << endl;
    find_last_not_ofDemo(str);
 
    return 0;
}

输出:

Original String : GeeksforGeeks
First unmatched character : s

语法6:从索引idx中查找str中的最后一个字符,该字符不等于char c。

size_type string::find_last_not_of (const char* cstr, size_type idx) const
c: Character c with which contents of str are required to be compared.
idx : index from where search of the first
unmatched character is to be started

CPP

// CPP code for size_type string::find_last_not_of
// (char c, size_type idx) const
 
#include 
using namespace std;
 
// Function to demonstrate find_last_not_of
void find_last_not_ofDemo(string str)
{
    // Finds last character in str from 6th index which
    // is not equal to character 'G'
    string::size_type ch = str.find_last_not_of('G', 6);
    cout << "First unmatched character : ";
    cout << str[ch];
}
 
// Driver code
int main()
{
    string str("GeeksforGeeks");
 
    cout << "Original String : " << str << endl;
    find_last_not_ofDemo(str);
 
    return 0;
}

输出:

Original String : GeeksforGeeks
First unmatched character : o

语法7:从索引idx开始,搜索最后一个字符,该字符也不是字符数组chars的chars_len字符的元素。

size_type string::find_last_not_of (const char* chars, size_type idx, size_type
chars_len) const
*chars : is the character array with which 
searching of first unmatched is to be performed.
idx : index number in string
chars_len : is the character length in 
*chars to be picked for searching purpose

CPP

// CPP code for size_type string::find_last_not_of
// (const char* chars, size_type idx, size_type chars_len) const
 
#include 
using namespace std;
 
// Function to demonstrate find_first_not_of
void find_last_not_ofDemo(string str)
{
    // Finds last character in str from 4th index which
    // is not equal to any of the first 3 characters from "svmnist"
    string::size_type ch = str.find_last_not_of("svmnist", 4, 3);
    cout << "First unmatched character : ";
    cout << str[ch];
}
 
// Driver code
int main()
{
    string str("GeeksforGeeks");
 
    cout << "Original String : " << str << endl;
    find_last_not_ofDemo(str);
 
    return 0;
}

输出:

Original String : GeeksforGeeks
First unmatched character : k
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”