logo

map find()-functie in C++ STL

De kaart::vind() is een ingebouwde functie in C++ STL die een iterator of een constante iterator retourneert die verwijst naar de positie waar de sleutel aanwezig is op de kaart. Als de sleutel niet aanwezig is in de kaartcontainer, retourneert deze een iterator of een constante iterator die verwijst naar kaart.end()

.
Syntaxis:



iterator=map_name.find(key) or constant iterator=map_name.find(key)>

Parameters: De functie accepteert één verplichte parameter sleutel, die de sleutel specificeert die moet worden doorzocht in de kaartcontainer.

Winstwaarde: De functie retourneert een iterator of een constante iterator die verwijst naar de positie waar de sleutel aanwezig is op de kaart. Als de sleutel niet aanwezig is in de kaartcontainer, retourneert deze een iterator of een constante iterator die verwijst naar map.end().

Tijdcomplexiteit voor zoekelement:
De tijdscomplexiteit voor het zoeken naar elementen in std::kaart is O(logboek n). Zelfs in het ergste geval zal dit O(log n) zijn, omdat elementen intern worden opgeslagen als een Balanced Binary Search-boom (BST), terwijl in std::unordered_map de beste en gemiddelde tijdcomplexiteit voor zoeken is O(1) omdat elementen zijn opgeslagen in een hashtabel en daarom fungeert de sleutel als index tijdens het zoeken in ongeordende kaarten. Maar in het slechtste geval is de tijdcomplexiteit voor zoeken O(N).



Hieronder ziet u de illustratie van de bovenstaande functie:

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

in.volgende java

>

>

Uitvoer

Element 2 : found : Value : 30 Element 5 : Not found>

Tijdcomplexiteit : O(log n)
Hulpruimte : Op)

Onderstaande code is een programma om alle elementen af ​​te drukken na het vinden van een element:

CPP




rasterindeling

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

>

>

Uitvoer

Elements from position of 3 in the map are : KEY ELEMENT 3 20 4 50>

Tijdcomplexiteit: O(logboek n)
Hulpruimte: Op)