In C++ find_first_of, as concerns a target string, means: find the first occurrence of a sequence of characters, in the target string. A particular sub-string may occur more than once. So, the job of the find_first_of() function is to find the first occurrence of that particular sub-string in the target string.
Consider the string,
The first character, ‘I’ is at index zero. The second, ‘t’ is at index 1; the third, which is a space, is at index 2; the fourth, ‘i’ is at index 3; and so on. “never” occurs twice in the string literal. ‘n’ for the first “never” occurs at index 11. ‘n’ for the second “never” occurs at a higher index. So, the first occurrence of “never” starts at index 11.
The above string has to be the list of a string object constructed from a string class. A C++ program to use the C++ find_first_of() function should begin with:
#include <string>
using namespace std;
The string library is included. find_first_of() is a member function of the string class. All code for the string for this article is done in the main() function.
There are five variants of the find_first_of() function. When there is more than one variant of a function, the function is said to be overloaded. This article explains how to use the overloaded find_first_of() member functions.
size_type find_first_of(charT c, size_type pos = 0)
This function returns the index of the first occurrence of a particular character (sub-string of one character) in the target string. The first parameter refers to the character. The second parameter indicates that the search starts from index 0 (default). The construction (instantiation) of the target string object can be:
Let the character to be searched for, be ‘n’. ‘n’ occurs more than once. In this case, the returned position is 11, as the first ‘n’ is at index 11.
The following code illustrates the use of this function, where the argument for the first parameter is an identifier:
char ss = 'n';
int i = str.find_first_of(ss);
cout << i << endl;
The output is 11.
The second parameter, “size_type pos = 0” in the function means that the search begins from zero. If the search were to begin from an index after the first ‘n’, then the index for the second ‘n’, would have been returned. The following program illustrates this with the first argument being a literal.
#include <string>
using namespace std;
int main()
{
string str = "It is that never say never again.";
int i = str.find_first_of('n', 17);
cout << i << endl;
return 0;
}
The output is 21.
size_type find_first_of(const charT* s, size_type pos = 0)
This function returns the index for the first character of the first occurrence of a particular sub-string (sequence of characters) in the target string. The first parameter refers to a constant pointer of character sequence (sub-string). The second parameter indicates that the search starts from index 0 (default, when the parameter is absent). The output of the following program is 11, with search beginning from zero:
#include <string>
using namespace std;
int main()
{
string str = "It is that never say never again.";
const char* ss = "never";
int i = str.find_first_of(ss);
cout << i << endl;
return 0;
}
size_type find_first_of(const T& t, size_type pos = 0)
This function returns the index for the first character of the first occurrence of a particular sub-string (sequence of characters), in the target string. The first parameter refers to an array-of-chars. The second parameter indicates that the search starts from index 0 by default. The output of the following program is 11, with search beginning from zero:
#include <string>
using namespace std;
int main()
{
string str = "It is that never say never again.";
char ss[] = "never";
int i = str.find_first_of(ss);
cout << i << endl;
return 0;
}
The output is 11. The statement,
could as well have been,
char ss[] = {'n','e','v','e','r','\0'};
Note the NUL character, ‘\0’ and its position. The literal, {‘n’,’e’,’v’,’e’,’r’,’\0′} can be used in the code in place of the identifier ss, as in the following program:
#include <string>
using namespace std;
int main()
{
string str = "It is that never say never again.";
int i = str.find_first_of({'n','e','v','e','r','\0'});
cout << i << endl;
return 0;
}
The output is still 11, as it should.
size_type find_first_of(const basic_string& str, size_type pos = 0)
This function is similar to the above, but the first argument is a string object constructed from the string class. In the following program, the first argument is the identifier of a string object constructed from the string class (search begins from zero):
#include <string>
using namespace std;
int main()
{
string str = "It is that never say never again.";
string ss = "never";
int i = str.find_first_of(ss);
cout << i << endl;
return 0;
}
The output is 11. The argument could have been the string construction, i.e. string(“never”) as in:
#include <string>
using namespace std;
int main()
{
string str = "It is that never say never again.";
int i = str.find_first_of(string("never"));
cout << i << endl;
return 0;
}
The output is 11 here. Remember that a string object can still be constructed as follows:
So, for this function, the first argument can still be a string literal, as in the following program:
#include <string>
using namespace std;
int main()
{
string str = "It is that never say never again.";
int i = str.find_first_of("never");
cout << i << endl;
return 0;
}
The output is 11.
size_type find_first_of(const charT* s, size_type pos, size_type n)
This function is similar to the above. If the third argument is less than or equal to the size of the sub-string but greater than 0, the function returns the expected number. The following code demonstrate the use of the third argument:
#include <string>
using namespace std;
int main()
{
string str = "It is that never say never again.";
int i = str.find_first_of("never", 17, 4);
cout << i << endl;
return 0;
}
The output is 21.
Sequence Not Found
What if the sub-string is not found? Consider the following program:
#include <string>
using namespace std;
int main()
{
string str = "It is that never say never again.";
int i = str.find_first_of("bcdfu");
cout << i << endl;
return 0;
}
The output is -1. If the sequence (sub-string) has no character in the target string, the return value of the function is -1. If the sequence is the empty string, the return value is still -1.
Consider the sequence,
“uehydyw”
And the target string,
“It is that never say never again.”
‘h’ in the sub-string, is the first character in the sub-string that occurs first in the target string. It occurs at index 7 in the target string.
So, the index that is returned from the target string, is the first character of the sub-string that matches the first occurrence of itself in the target string. The following program illustrates this:
#include <string>
using namespace std;
int main()
{
string str = "It is that never say never again.";
int i = str.find_first_of("uehydyw");
cout << i << endl;
return 0;
}
The output is 7.
What if the sequence of characters is longer than the target string? Consider the following program:
#include <string>
using namespace std;
int main()
{
string str = "abcde";
int i = str.find_first_of("abcdefghijk");
cout << i << endl;
return 0;
}
The output is 0. This is because the target string is actually the first sequence of characters of the sequence (sub-string). And what if the sequence was longer than the target string and no character of the sequence exists in the target string? Consider the following:
#include <string>
using namespace std;
int main()
{
string str = "abcde";
int i = str.find_first_of("fghijklmnop");
cout << i << endl;
return 0;
}
The output is -1, still in conformity with the fact that, if the sequence is not found in the target string, then the return value of the function would be -1.
Conclusion
find_first_of() is a member function of the string class. It returns the index of the first occurrence of a sequence of characters found in a target string. Index counting begins from 0. This sequence can be called a sub-string. If a sequence is not found, -1 is returned. The find_first_of() member function is overloaded in five ways. The different overloaded functions take different character sequence formats. For all these overloaded functions, searching beginning from 0 is the default. However, each has an argument that can allow search to begin from any index ahead.