The C++ find_first_of() code sample 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_of() function to search through a string for the first character that matches any element of a specified string

To show: How to search through a string for the first character that matches any element of a specified string using find_first_of() in C++ programming part II

 

// the C++ find_first_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 456...Testing 456...789");

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

basic_string <char>::size_type index5, index6;

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

const char *cstr2 = "t6";

index5 = str3.find_first_of(cstr2);

 

cout<<"Operation: str3.find_first_of(cstr2)"<<endl;

if(index5 != npos)

cout<<"The index of the 1st occurrence of an "

<<"element of 't6' in str3 after the 0th "

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

else

cout<<"Elements of the substring 't6' were not "

<<"found in str3 after the 0th position."<<endl;

const char *cstr3 = "t68";

index6 = str3.find_first_of(cstr3, index5 + 1, 2);

 

cout<<"\nOperation: str3.find_first_of(cstr3, index5 + 1, 2)"<<endl;

if(index6 != npos)

cout<<"The index of the second occurrence of an "

<<"element of 't68' in str3 after the 0th "

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

else

cout<<"Elements of the substring 't68' were not "

<<"found in str3 after the first occurrence."<<endl;

cout<<endl;

 

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

string str4("find_first_of() and find_first_of()");

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

basic_string <char>::size_type index7, index8;

string str5("dfz");

index7 = str5.find_first_of(str5, 3);

 

cout<<"Operation: str5.find_first_of(str5, 3)"<<endl;

if(index7 != npos)

cout<<"The index of the 1st occurrence of an"

<<" element of 'dfz' in str4 after the 3rd "

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

else

cout<<"Elements of the substring 'dfz' were not "

<<"found in str4 after the 3rd position."<<endl;

string str6("fo");

index8 = str4.find_first_of(str6);

 

cout<<"\nOperation: str4.find_first_of(str6)"<<endl;

if(index8 != npos)

cout<<"The index of the 1st occurrence of an"

<<" element of 'fo' in str4 after the 0th "

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

else

cout<<"Elements of the substring 'fo' were not "

<<"found in str4 after the 0th position."<<endl;

 

return 0;

}

 

Output example:

 

str3 string is: Testing 456...Testing 456...789

Operation: str3.find_first_of(cstr2)

The index of the 1st occurrence of an element of 't6' in str3 after the 0th position is: 3

 

Operation: str3.find_first_of(cstr3, index5 + 1, 2)

The index of the second occurrence of an element of 't68' in str3 after the 0th position is: 10

 

str4 string is: find_first_of() and find_first_of()

Operation: str5.find_first_of(str5, 3)

Elements of the substring 'dfz' were not found in str4 after the 3rd position.

 

Operation: str4.find_first_of(str6)

The index of the 1st occurrence of an element of 'fo' in str4 after the 0th position is: 0

Press any key to continue . . .

 

 

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