Der map::find() ist eine integrierte Funktion in C++ STL, die einen Iterator oder einen konstanten Iterator zurückgibt, der auf die Position verweist, an der der Schlüssel in der Karte vorhanden ist. Wenn der Schlüssel nicht im Map-Container vorhanden ist, wird ein Iterator oder ein konstanter Iterator zurückgegeben, auf den verwiesen wird map.end()
.
Syntax:
iterator=map_name.find(key) or constant iterator=map_name.find(key)>
Parameter: Die Funktion akzeptiert einen obligatorischen Parameter Schlüssel, Gibt den Schlüssel an, nach dem im Kartencontainer gesucht werden soll.
Rückgabewert: Die Funktion gibt einen Iterator oder einen konstanten Iterator zurück, der auf die Position verweist, an der der Schlüssel in der Karte vorhanden ist. Wenn der Schlüssel nicht im Map-Container vorhanden ist, wird ein Iterator oder ein konstanter Iterator zurückgegeben, der auf map.end() verweist.
Zeitkomplexität für die Suche nach Elementen:
Die zeitliche Komplexität für die Suche nach Elementen in std::map ist O(log n). Selbst im schlimmsten Fall wird es O(log n) sein, da Elemente intern als Balanced Binary Search Tree (BST) gespeichert werden, während in std::unordered_map Im besten Fall und im durchschnittlichen Fall beträgt die Zeitkomplexität für die Suche O(1), da Elemente in einer Hash-Tabelle gespeichert werden und der Schlüssel daher bei der Suche in ungeordneten Karten als Index fungiert. Im schlimmsten Fall beträgt die Zeitkomplexität für die Suche jedoch O(N).
Unten sehen Sie die Abbildung der obigen Funktion:
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; }> |
Konvertieren Sie einen String in eine Enumeration
>
>Ausgabe
Vicky Kaushal Alter
Element 2 : found : Value : 30 Element 5 : Not found>
Zeitkomplexität : O(log n)
Hilfsraum : An)
Der folgende Code ist ein Programm zum Drucken aller Elemente, nachdem ein Element gefunden wurde:
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; }> |
>
>Ausgabe
Elements from position of 3 in the map are : KEY ELEMENT 3 20 4 50>
Zeitkomplexität: O(log n)
Hilfsraum: An)