logo

recherche de chaîne en C++

Chaîne find est utilisé pour rechercher la première occurrence d'une sous-chaîne dans la chaîne spécifiée appelée. Il renvoie l'index de la première occurrence de la sous-chaîne dans la chaîne à partir de la position de départ donnée. La valeur par défaut de la position de départ est 0.

C'est une fonction membre de std :: chaîne classe.



Syntaxe:

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>

Paramètres:

    str : La sous-chaîne à rechercher. s : La sous-chaîne à rechercher, donnée sous forme de chaîne de style C. pos : La position initiale à partir de laquelle la recherche de chaîne doit commencer.

Valeur de retour :



  • La fonction renvoie l'index de la première occurrence de la sous-chaîne.
  • Si la sous-chaîne n'est pas trouvée, elle renvoie string::npos (string::pos est un membre statique avec sa valeur la plus élevée possible pour le type de données size_t).

Analyse de complexité :

    Complexité temporelle : O(N*M), où N est la taille de la chaîne (str) et M est la taille de la sous-chaîne à rechercher. Espace auxiliaire : O(1)

Exemple:

C++




nombre premier java



c nombre aléatoire
// 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;> }>

>

>

Sortir

First occurrence is 0 Next occurrence is 8>

Trouver l'occurrence d'un personnage

Nous pouvons également utiliser la fonction find pour rechercher l’occurrence d’un seul caractère dans la chaîne.

Syntaxe:

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

Ici, c est le caractère à rechercher.

Exemple:

C++




trier une liste de tableaux
// 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;> }>

>

>

Sortir

listes CSS
First occurrence is 0 Next occurrence is 8>

Rechercher une chaîne partielle

Nous pouvons également rechercher une partie de la chaîne donnée.

Syntaxe:

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

Ici, n est le nombre de caractères à faire correspondre.

Exemple:

C++




onclick javascript

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

>

>

Sortir

0>