The C++ find_first_of() member function 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_of() member function to search through a string for the first character that matches any element of a specified string part I
To show: How to use the C++ find_first_of() member function to search through a string for the first character that matches any element of a specified string part I
// the C++ find_first_of() program example part I
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
// searching for a single character in a string
string str1("find_first_of()");
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_of('r', 3);
cout<<"Operation: str1.find_first_of('r', 3)"<<endl;
if(index1 != npos)
cout<<"The index of the 1st 'r' found after the 3rd "
<<"position in str1 is: "<<unsigned int(index1)<<endl;
else
cout<<"The character 'r' was not found in str1"<<endl;
index2 = str1.find_first_of('z');
cout<<"\nOperation: str1.find_first_of('z')"<<endl;
if(index2 != npos)
cout<<"The index of the 'z' found in str1 is: "<<unsigned int(index2)<<endl;
else
cout<<"The character 'z' was not found in str1."<<endl;
// searching a string for a substring as specified by a C-string
string str2("Testing 123...Testing 123");
cout<<"\nstr2 string is: "<<str2<<endl;
basic_string <char>::size_type index3, index4;
const char *cstr = "s1";
index3 = str2.find_first_of(cstr, 3);
cout<<"Operation: str2.find_first_of(cstr, 3)"<<endl;
if(index3 != npos)
cout<<"The index of the 1st occurrence of an "
<<"element of 's1' in str2 after the 3rd "
<<"position is: "<<unsigned int(index3)<<endl;
else
cout<<"Elements of the substring 's1' were not "
<<"found in str2 after the 3rd position."<<endl;
const char *cstr1 = "g3";
index4 = str2.find_first_of(cstr1);
cout<<"\nOperation: str2.find_first_of(cstr1)"<<endl;
if(index4 != npos)
cout <<"The index of the 1st element of 'g3' "
<<"after the 0th position in str2 is: "<<unsigned int(index4)<<endl;
else
cout<<"The substring 'g3' was not found in str2."<<endl;
return 0;
}
Output example:
str1 string is: find_first_of()
Operation: str1.find_first_of('r', 3)
The index of the 1st 'r' found after the 3rd position in str1 is: 7
Operation: str1.find_first_of('z')
The character 'z' was not found in str1.
str2 string is: Testing 123...Testing 123
Operation: str2.find_first_of(cstr, 3)
The index of the 1st occurrence of an element of 's1' in str2 after the 3rd position is: 8
Operation: str2.find_first_of(cstr1)
The index of the 1st element of 'g3' after the 0th position in str2 is: 6
Press any key to continue . . .