Snor find bruges til at finde den første forekomst af en understreng i den angivne streng, der kaldes på. Det returnerer indekset for den første forekomst af understrengen i strengen fra den givne startposition. Standardværdien for startpositionen er 0.
Det er en medlemsfunktion af std::streng klasse.
Syntaks:
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 : Den understreng, der skal søges i. s : Den understreng, der skal søges i, angivet som en C-stil streng. pos : Startpositionen, hvorfra strengsøgningen skal begynde.
Returneringsværdi:
- Funktionen returnerer indekset for den første forekomst af understrengen.
- Hvis understrengen ikke findes, returnerer den string::npos(streng::pos er et statisk medlem med dens værdi som den højest mulige for datatypen size_t).
Kompleksitetsanalyse:
- Tidskompleksitet: O(N*M), hvor N er størrelsen på strengen(str), og M er størrelsen på den understreng, der skal søges i. Hjælpeplads: O(1)
Eksempel:
C++
git push-kommando
// 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;> }> |
>
>Produktion
java farvekoder
First occurrence is 0 Next occurrence is 8>
Find forekomsten af en karakter
Vi kan bruge find-funktionen til også at finde forekomsten af et enkelt tegn i strengen.
Syntaks:
size_t find (const char c, size_t pos = 0);>
Her, c er det tegn, der skal søges efter.
Eksempel:
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;> }> |
>
>Produktion
First occurrence is 0 Next occurrence is 8>
Søg efter en delstreng
Vi kan også søge efter en del af den givne streng.
Syntaks:
size_t find (const char *str, size_t pos, size_t n);>
Her, n er antallet af tegn, der skal matches.
Eksempel:
oprette forbindelse til databasen i java
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;> }> |
>
>Produktion
0>