The C++ find_first_not_of() program example part I

 

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

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 I

 

// the C++ find_first_not_of() code sample part I

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

// searching a single character in a string

string str1("Testing the find_first_not_of() part 1");

cout<<"str1 string is: "<<str1<<endl;

basic_string <char>::size_type index1, index2;

static const basic_string <char>::size_type npos = -1;

index1 = str1.find_first_not_of('_', 3);

 

cout<<"Operation: str1.find_first_not_of('_', 3)"<<endl;

if(index1 != npos)

cout<<"The index of the 1st '_' found after the 3rd position in str1 is: "<<unsigned int(index1)<<endl;

else

cout<<"The character '_' was not found in str1"<<endl;

index2 = str1.find_first_not_of('T');

 

cout<<"\nOperation: str1.find_first_not_of('T')"<<endl;

if(index2 != npos)

cout<<"The index of the 'non T' found in str1 is: "<<unsigned int(index2)<<endl;

else

cout<<"The character 'non T' was not found in str1."<<endl;

cout<<endl;

 

// searching a string for a substring as specified by a C-string

string str2("Testing the find_first_not_of() part 2");

cout<<"str2 string is: "<<str2<<endl;

basic_string <char>::size_type index3, index4;

const char *cstr2 = "df";

index3 = str2.find_first_not_of(cstr2, 4);

 

cout<<"Operation: str2.find_first_not_of(cstr2, 4)"<<endl;

if(index3 != npos)

cout<<"The index of the 1st occurrence of an element of 'df' in str2 after the 4th "

<<"position is: "<<unsigned int(index3)<<endl;

else

cout<<"Elements of the substring 'df' were not found in str2 after the 4th position."<<endl;

const char *cstr3 = "gz";

index4 = str2.find_first_not_of(cstr3);

 

cout<<"\nOperation: str2.find_first_not_of(cstr3)"<<endl;

if(index4 != npos)

cout<<"The index of the 1st element of 'gz' after the 0th position in str2 is: "

<<unsigned int(index4)<<endl;

else

cout<<"The substring 'gz' was not found in str2"<<endl;

 

return 0;

}

 

Output example:

 

str1 string is: Testing the find_first_not_of() part 1

Operation: str1.find_first_not_of('_', 3)

The index of the 1st '_' found after the 3rd position in str1 is: 3

 

Operation: str1.find_first_not_of('T')

The index of the 'non T' found in str1 is: 1

 

str2 string is: Testing the find_first_not_of() part 2

Operation: str2.find_first_not_of(cstr2, 4)

The index of the 1st occurrence of an element of 'df' in str2 after the 4th position is: 4

 

Operation: str2.find_first_not_of(cstr3)

The index of the 1st element of 'gz' after the 0th position in str2 is: 0

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index