Snaar find wordt gebruikt om de eerste keer dat een subtekenreeks voorkomt in de opgegeven tekenreeks die wordt aangeroepen, te vinden. Het retourneert de index van de eerste keer dat de subtekenreeks in de tekenreeks voorkomt vanaf de opgegeven startpositie. De standaardwaarde van de startpositie is 0.
Het is een ledenfunctie van std::tekenreeks klas.
Syntaxis:
blokkeer advertenties op YouTube Android
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>
Parameters:
- str : De subtekenreeks waarin moet worden gezocht. s : De subtekenreeks waarnaar moet worden gezocht, gegeven als een tekenreeks in C-stijl. pos : De beginpositie vanwaar het zoeken naar de tekenreeks moet beginnen.
Winstwaarde:
- De functie retourneert de index van de eerste keer dat de subtekenreeks voorkomt.
- Als de subtekenreeks niet wordt gevonden, retourneert deze string::npos(string::pos is een statisch lid waarvan de waarde de hoogst mogelijke is voor het gegevenstype size_t).
Complexiteitsanalyse:
- Tijdcomplexiteit: O(N*M), waarbij N de grootte is van de string(str) en M de grootte is van de substring waarin moet worden gezocht. Hulpruimte: O(1)
Voorbeeld:
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;> }> |
bijenkorf architectuur
>
>Uitvoer
First occurrence is 0 Next occurrence is 8>
Zoek het voorkomen van een teken
We kunnen de find-functie gebruiken om ook het voorkomen van een enkel teken in de string te vinden.
Syntaxis:
size_t find (const char c, size_t pos = 0);>
Hier, C is het teken waarnaar moet worden gezocht.
Voorbeeld:
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;> }> |
>
>Uitvoer
hoe u de schermgrootte kunt controleren
First occurrence is 0 Next occurrence is 8>
Zoek naar een gedeeltelijke tekenreeks
We kunnen ook zoeken naar een deel van de gegeven string.
Syntaxis:
size_t find (const char *str, size_t pos, size_t n);>
Hier, N is het aantal overeenkomende tekens.
Voorbeeld:
C++
sorteer arraylijst 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;> }> |
>
>Uitvoer
0>