The C++ find_first_not_of() member function code example part II
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Using the C++ find_first_not_of() member function to search through a string for the first character that is not any element of a specified string part II
To show: How to use the C++ find_first_not_of() member function to search through a string for the first character that is not any element of a specified string part II
// the C++ find_first_not_of() code sample part II
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
// searching a string for a substring as specified by a C-string
string str3("Testing the find_first_not_of() part 3");
cout<<"str3 string is: "<<str3<<endl;
basic_string <char>::size_type index5, index6;
static const basic_string <char>::size_type npos = -1;
const char *cstr4 ="nro";
index5 = str3.find_first_not_of(cstr4);
cout<<"Operation: str3.find_first_not_of(cstr4)"<<endl;
if(index5 != npos)
cout<<"The index of the 1st occurrence of an "
<<"element in str3 other than one of the "
<<"characters in 'nro' is: "<<unsigned int(index5)<<endl;
else
cout<<"Elements in str3 contain only characters "
<<" in the string 'nro'. "<<endl;
const char *cstr5 ="nro";
index6 = str3.find_first_not_of(cstr5, index5+1, 2);
cout<<"\nOperation: str3.find_first_not_of(cstr5, index5+1, 2)"<<endl;
if(index6 != npos)
cout<<"The index of the second occurrence of an "
<<"element of 'nro' in str3 after the 0th "
<<"position is: "<<unsigned int(index6)<<endl<<endl;
else
cout<<"Elements in str3 contain only characters "
<<" in the string 'nro'"<<endl;
cout<<endl;
// searching a string for a substring as specified by a string
string str4("Testing the find_first_not_of() part 4");
cout<<"str4 string is: "<<str4<<endl;
basic_string <char>::size_type index7, index8;
string str5("tf7");
index7 = str4.find_first_not_of(str5, 3);
cout<<"Operation: str4.find_first_not_of(str5, 3)"<<endl;
if(index7 != npos)
cout<<"The index of the 1st non occurrence of an element of 'tf7' "
<<"in str4 after the 3rd position is: "<<unsigned int(index7)<<endl;
else
cout<<"Elements other than those in the substring 'tf7' "
<<"were not found in the string str4."<<endl;
string str6("in");
index8 = str4.find_first_not_of(str6);
cout<<"\nOperation: str4.find_first_not_of(str6)"<<endl;
if(index8 != npos)
cout<<"The index of the 1st occurrence of an "
<<"element of 'in' in str4 after the 0th "
<<"position is: "<<unsigned int(index8)<<endl;
else
cout<<"Elements other than those in the substring"
<<" 'in' were not found in the string str4."<<endl;
return 0;
}
Output example:
str3 string is: Testing the find_first_not_of() part 3
Operation: str3.find_first_not_of(cstr4)
The index of the 1st occurrence of an element in str3 other than one of the characters in 'nro' is: 0
Operation: str3.find_first_not_of(cstr5, index5+1, 2)
The index of the second occurrence of an element of 'nro' in str3 after the 0th position is: 1
str4 string is: Testing the find_first_not_of() part 4
Operation: str4.find_first_not_of(str5, 3)
The index of the 1st non occurrence of an element of 'tf7' in str4 after the 3rd position is: 4
Operation: str4.find_first_not_of(str6)
The index of the 1st occurrence of an element of 'in' in str4 after the 0th position is: 0
Press any key to continue . . .