El mapa::find() és una funció integrada en C++ STL que retorna un iterador o un iterador constant que fa referència a la posició on la clau està present al mapa. Si la clau no està present al contenidor del mapa, retorna un iterador o un iterador constant que fa referència a map.end()
.
Sintaxi:
iterator=map_name.find(key) or constant iterator=map_name.find(key)>
Paràmetres: La funció accepta un paràmetre obligatori clau, que especifica la clau que s'ha de cercar al contenidor del mapa.
Valor de retorn: La funció retorna un iterador o un iterador constant que fa referència a la posició on la clau està present al mapa. Si la clau no està present al contenidor del mapa, retorna un iterador o un iterador constant que fa referència a map.end().
Complexitat temporal per a l'element de cerca:
La complexitat temporal per cercar elements std::mapa és O(log n). Fins i tot en el pitjor dels casos, serà O(log n) perquè els elements s'emmagatzemen internament com a arbre de cerca binària equilibrada (BST), mentre que, en std::mapa_desordenat El millor cas i la complexitat mitjana del temps per a la cerca és O(1) perquè els elements s'emmagatzemen en una taula Hash i, per tant, la clau actua com un índex mentre es cerca en mapes no ordenats. Però la complexitat temporal en el pitjor dels casos per a la cerca és O(N).
A continuació es mostra la il·lustració de la funció anterior:
C++
// C++ program for illustration> // of map::find() function> #include> using> namespace> std;> int> main()> {> >// Initialize container> >map<>int>,>int>>m;> >// Insert elements in random order> >m.insert({ 2, 30 });> >m.insert({ 1, 40 });> >m.insert({ 3, 20 });> >m.insert({ 4, 50 });> >int> s1=2;>//element1 to find (exist in the map)> >int> s2=5;>//element2 to find (does not exist in the map)> > >cout <<>'Element '>< if(m.find(s1)!=m.end()){ //if the element is found before the end of the map cout<<' : found : Value : '< //if the element is present then you can access it using the index } else cout<<' : Not found'< cout << 'Element '< if(m.find(s2)!=m.end()){ //if the element is found before the end of the map cout<<' : found : Value : '< //if the element is present then you can access it using the index } else cout<<' : Not found'< return 0; }> |
booleà a cadena java
>
>Sortida
Element 2 : found : Value : 30 Element 5 : Not found>
Complexitat temporal : O(log n)
Espai Auxiliar : O(n)
A continuació hi ha un programa per imprimir tots els elements després de trobar un element:
com centrar una imatge en CSS
CPP
// C++ program for illustration> // of map::find() function> #include> using> namespace> std;> int> main()> {> >// Initialize container> >map<>int>,>int>>mp;> >// Insert elements in random order> >mp.insert({ 2, 30 });> >mp.insert({ 1, 40 });> >mp.insert({ 3, 20 });> >mp.insert({ 4, 50 });> >cout <<>'Elements from position of 3 in the map are :
'>;> >cout <<>'KEY ELEMENT
'>;> >// find() function finds the position> >// at which 3 is present> >for> (>auto> itr = mp.find(3); itr != mp.end(); itr++) {> > >cout ' ' '
'; } return 0; }> |
>
>Sortida
Elements from position of 3 in the map are : KEY ELEMENT 3 20 4 50>
Complexitat temporal: O(log n)
Espai auxiliar: O(n)