logo

iskanje niza v C++

Vrvica find se uporablja za iskanje prve pojavitve podniza v navedenem klicanem nizu. Vrne indeks prve pojavitve podniza v nizu z danega začetnega položaja. Privzeta vrednost začetnega položaja je 0.

Je članska funkcija std::niz razred.



Sintaksa:

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>

Parametri:

    str : podniz za iskanje. s : Podniz za iskanje, podan kot niz v slogu C. pos : začetni položaj, od koder naj se začne iskanje niza.

Povratna vrednost:



  • Funkcija vrne indeks prve pojavitve podniza.
  • Če podniza ni mogoče najti, vrne string::npos(string::pos je statični član z najvišjo možno vrednostjo za podatkovni tip size_t).

Analiza kompleksnosti:

    Časovna zahtevnost: O(N*M), kjer je N velikost niza (str) in M ​​velikost podniza, ki ga je treba iskati. Pomožni prostor: O(1)

primer:

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;> }>

>

python rstrip

>

Izhod

First occurrence is 0 Next occurrence is 8>

Poiščite pojav znaka

Funkcijo find lahko uporabimo tudi za iskanje pojavljanja posameznega znaka v nizu.

Sintaksa:

size_t find (const char c, size_t pos = 0);>

tukaj, c je znak, ki ga je treba iskati.

primer:

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;> }>

>

indijska igralka rani mukerji

>

Izhod

First occurrence is 0 Next occurrence is 8>

Poiščite delni niz

Poiščemo lahko tudi del danega niza.

Sintaksa:

size_t find (const char *str, size_t pos, size_t n);>

tukaj, n je število znakov, ki se ujemajo.

primer:

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;> }>

>

>

Izhod

0>