Tětiva find se používá k nalezení prvního výskytu dílčího řetězce v zadaném volaném řetězci. Vrací index prvního výskytu podřetězce v řetězci z dané počáteční pozice. Výchozí hodnota počáteční pozice je 0.
Je to členská funkce std::string třída.
Syntax:
size_t find (const string& str, size_t pos = 0); // for C++ Style Strings or size_t find (const char* s, size_t pos = 0); // for C-Style Strings>
Parametry:
- str : Podřetězec, který má být prohledán. s : Podřetězec, který má být prohledán, zadaný jako řetězec ve stylu C. pos : Počáteční pozice, od které má začít hledání řetězce.
Návratová hodnota:
- Funkce vrátí index prvního výskytu podřetězce.
- Pokud podřetězec není nalezen, vrací string::npos(string::pos je statický člen s nejvyšší možnou hodnotou pro datový typ size_t).
Analýza složitosti:
jak vycentrovat obrázek na css
- Časová složitost: O(N*M), kde N je velikost řetězce (str) a M je velikost podřetězce, který má být prohledán. Pomocný prostor: O(1)
Příklad:
C++
// C++ program to demonstrate> // working of string.find()> #include> #include> using> namespace> std;> // Driver code> int> main()> {> >string str =>'geeksforgeeks a computer science'>;> >string str1 =>'geeks'>;> > >// Find first occurrence of 'geeks'> >size_t> found = str.find(str1);> >if> (found != string::npos)> >cout <<>'First occurrence is '> <<> >found << endl;> > >// Find next occurrence of 'geeks'.> >// Note here we pass> >// 'geeks' as C style string.> >char> arr[] =>'geeks'>;> >found = str.find(arr, found+1);> >if> (found != string::npos)> >cout <<>'Next occurrence is '> <<> >found << endl;> >return> 0;> }> |
>
>Výstup
First occurrence is 0 Next occurrence is 8>
Najděte výskyt znaku
Pomocí funkce find můžeme také najít výskyt jednoho znaku v řetězci.
příklady kódu c#
Syntax:
size_t find (const char c, size_t pos = 0);>
Tady, C je znak, který se má hledat.
Příklad:
C++
// C++ program to demonstrate> // working of string find> // to find occurrence of> // a character> #include> #include> using> namespace> std;> // Driver code> int> main()> {> >string str =>'geeksforgeeks a computer science'>;> >char> c =>'g'>;> >// Find first occurrence of 'g'> >size_t> found = str.find(c);> >if> (found != string::npos)> >cout <<>'First occurrence is '> <<> >found << endl;> > >// Find next occurrence of 'g'> >found = str.find(c, found + 1);> >if> (found != string::npos)> >cout <<>'Next occurrence is '> <<> >found << endl;> >return> 0;> }> |
>
>Výstup
First occurrence is 0 Next occurrence is 8>
Vyhledejte částečný řetězec
Můžeme také vyhledat část zadaného řetězce.
Syntax:
size_t find (const char *str, size_t pos, size_t n);>
Tady, n je počet znaků, které mají odpovídat.
execvp
Příklad:
C++
// C++ program to demonstrate> // working of string find to> // search a string> #include> #include> using> namespace> std;> // Driver code> int> main()> {> >string str =>'geeksforgeeks a computer science'>;> > >// Only search first 5 characters> >// of 'geeks.practice'> >size_t> found = str.find(>'geeks.practice'>,> >0, 5);> >if> (found != string::npos)> >cout << found << endl;> >return> 0;> }> |
filmy 123 až
>
>Výstup
0>