Reťazec find sa používa na nájdenie prvého výskytu podreťazca v zadanom volanom reťazci. Vráti index prvého výskytu podreťazca v reťazci z danej počiatočnej pozície. Predvolená hodnota počiatočnej pozície je 0.
Ide o členskú funkciu o std::string trieda.
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>
Parametre:
- str : Podreťazec, ktorý sa má vyhľadať. s : Podreťazec, ktorý sa má vyhľadať, zadaný ako reťazec v štýle C. pos : Počiatočná pozícia, od ktorej má začať vyhľadávanie reťazca.
Návratová hodnota:
- Funkcia vráti index prvého výskytu podreťazca.
- Ak sa podreťazec nenájde, vráti string::npos(string::pos je statický člen s najvyššou možnou hodnotou pre dátový typ size_t).
Analýza zložitosti:
np.argmax
- Časová zložitosť: O(N*M), kde N je veľkosť reťazca (reťazca) a M je veľkosť podreťazca, ktorý sa má hľadať. Pomocný priestor: O(1)
Prí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;> }> |
>
>
reťazec java concatVýkon
First occurrence is 0 Next occurrence is 8>
Nájdite výskyt znaku
Pomocou funkcie find môžeme nájsť aj výskyt jedného znaku v reťazci.
Syntax:
size_t find (const char c, size_t pos = 0);>
Tu, c je znak, ktorý sa má hľadať.
Príklad:
C++
pre slučky java
// 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;> }> |
pawandeep rajan
>
>Výkon
First occurrence is 0 Next occurrence is 8>
Vyhľadajte čiastočný reťazec
Môžeme tiež vyhľadať časť zadaného reťazca.
Syntax:
size_t find (const char *str, size_t pos, size_t n);>
Tu, n je počet znakov, ktoré sa majú zhodovať.
Príklad:
C++
ako previesť int na reťazec java
// 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;> }> |
>
>Výkon
0>