public static member function
<string>
static const char_type* find (const char_type* p, size_t n, const char_type& c);
Find first occurrence of character
Returns a pointer to the first character in the sequence of n characters pointed by p that compares equal to c.
All character traits types shall implement the function as if the individual characters were compared using member eq.
Parameters
- p
- Pointer to an array with a sequence of characters.
Notice that the function will consider that the length of the sequences is n characters, independently on whether it contains null-characters or not.
- n
- Length (in characters) of the sequence of characters to compare.
- c
- A character value.
Member type char_type is the character type (i.e., the class template parameter in char_traits).
size_t is an unsigned integral type.
Return Value
A pointer to the first match, if any, or a null pointer otherwise.
Example
1 2 3 4 5 6 7 8 9 10 11
|
// char_traits::find
#include <iostream> // std::cout
#include <string> // std::char_traits
int main ()
{
const char foo[] = "test string";
const char* p = std::char_traits<char>::find(foo,std::char_traits<char>::length(foo),'i');
if (p) std::cout << "the first 'i' in \"" << foo << "\" is at " << (p-foo) << ".\n";
return 0;
}
| |
Output:
the first 'i' in "test string" is at 8.
|
Complexity
Up to linear in n.
Exception safety
Unless either p does not point to an array long enough, this member function never throws exceptions (no-throw guarantee) in any of the standard specializations.
Otherwise, it causes undefined behavior.