The nesređena karta je pridruženi spremnik koji sadrži elemente stvorene spajanjem mapirane vrijednosti s vrijednošću ključa. Element se identificira specifično po svojoj ključna vrijednost , i mapirana vrijednost je sadržaj povezan s ključem. Ključevi i vrijednosti mogu biti bilo kojeg utvrđenog ili korisnički definirana vrsta . Neuređenu mapu možemo zamisliti kao podatkovnu strukturu tipa rječnika koja u sebi pohranjuje elemente. Sekvencijalni parovi koje sadrži (ključ, vrijednost) omogućiti brzo pronalaženje određenog elementa koristeći njegov pojedinačni ključ.
Ključ priložen karti je raspršeno u indekse hash tablice, zbog čega brzina strukture podataka uvelike ovisi o hash funkciji, ali u prosjeku, trošak pretraživati, umetati i brisati iz hash tablice je o(1).
java matematika.min
U najgorem slučaju, posebno za velike proste cijele brojeve, to je vremenska složenost može varirati od o(1) do na) . U ovom slučaju preporučuje se korištenje karte kako biste izbjegli primanje datoteke (vremensko ograničenje premašeno) problem.
Sintaksa:
Unordered_mapumap
Primjer:
//A c++ program to check an unordered map in it. #include #include using namespace std; int main() { unordered_mapumap; umap['javatpoint'] = 20; umap['regular'] = 30; umap['distribute'] = 40; for (auto y :umap) cout<<y.first<< ' << y.second<<endl; } < pre> <p> <strong>Output</strong> </p> <pre> Distribute 40 Regular 30 Javatpoint 20 </pre> <p> <strong>Explanation:</strong> </p> <p>This output specifically justifies the fact that the <strong> <em>unordered map's</em> </strong> output value is generated in a random <strong> <em>key-to-value</em> </strong> manner while the map shows value and key in an ordered fashion.</p> <h2>Unordered set vs Unordered map</h2> <p>Some differences between Unordered set and Unordered map are as follows:</p> <h3>Unordered map</h3> <ul> <li>Only <strong> <em>(key-value)</em> </strong> pairs are found in the elements of an <strong> <em>unordered map</em> </strong> .</li> <li>Use the operator <strong>'[]'</strong> to extract a key's corresponding value from a map.</li> </ul> <h3>Unordered set</h3> <ul> <tr><td> <em>Key-value</em> </td> pairs are mostly utilised to determine whether a set is present or absent and are not always present in an unordered set. <li>Using the <strong> <em>find() function</em> </strong> , an element is searched for. Thus, there is no need for an operator.</li> </tr></ul> <p> <strong>Important point:</strong> </p> <p>For instance, take the issue of counting the frequency of individual words. Since, counts cannot be stored in <strong> <em>unordered set (or set),</em> </strong> we must instead use unordered map.</p> <h2>Map vs. Unordered map</h2> <p>Some differences between the Map and Unordered map are as follows:</p> <h3>Unordered map</h3> <ul> <li>Any order may be used to store the unordered map key.</li> <li>The implementation of unordered map results in an uneven tree structure, making it impossible to retain the order of the entries.</li> <li>Operations on an unordered map typically have an <strong> <em>o(1) time complexity</em> </strong> .</li> </ul> <h3>Map</h3> <ul> <li>The map is an ordered list of distinct keys.</li> <li>It is possible to preserve the elements' order (by specific tree traversal) because map uses a balanced tree structure.</li> <li>The map operations have an <strong> <em>o time complexity (log n)</em> </strong> .</li> </ul> <h2>Procedures for unordered map</h2> <p>There are numerous functions that can be used with unordered map. The ones who are most helpful are:</p> <ul> <li>Operator =</li> <li>Operator[]</li> <li>Beginning and ending of the iterator</li> <li>Empty</li> <li>Size of the capacity</li> <li>For a lookup, locate and count.</li> <li>Insert and delete</li> </ul> <p>The full list of an unordered map's methods is shown below:</p> <p> <strong>At():</strong> </p> <p>This c++ unordered map method <strong> <em>returns</em> </strong> a reference to the value with the specified element as the <strong> <em>key k</em> </strong> .</p> <p> <strong>Begin():</strong> </p> <p>It provides a return value that is an <strong> <em>iterator pointing</em> </strong> to the first entry in the unordered map container.</p> <p> <strong>End():</strong> </p> <p>The unordered map container bucket returns an <strong> <em>iterator pointing</em> </strong> to the location after the final element ().</p> <p> <strong>Bucket():</strong> </p> <p>It returns the bucket number in the map's bucket count where the element with <strong> <em>key k</em> </strong> is placed.</p> <p> <strong>Bucket_count()</strong> </p> <p>The unordered map's total number of buckets is <strong> <em>tallied</em> </strong> using the bucket count function. It can be called without passing any parameters.</p> <p> <strong>Bucket size</strong> </p> <p>It gives the unordered map count's element count for each <strong> <em>bucket ()</em> .</strong> </p> <p> <strong>Count()</strong> </p> <p>It gives the unordered map count's element count for each <strong> <em>bucket ()</em> </strong> the number of elements in an unordered map with the specified key equal range should be counted.</p> <p> <strong>Equal_eange()</strong> </p> <p>It returns the boundaries of a range with all the container's items and a key that compares to <strong> <em>k</em> </strong> .</p> <p> <strong>Find()</strong> </p> <p>Gives an iterator to the element's empty.</p> <p> <strong>Position ()</strong> </p> <p>It determines whether the unordered map container's container is empty.</p> <p> <strong>Erase()</strong> </p> <p>Elements in the unordered map container can be deleted using the <strong> <em>erase()</em> </strong> function.</p> <p>Although the functions to view the internal bucket size, bucket count, used hash function, and various hash policies are also provided by the <strong> <em>c++11 library</em> </strong> , they are less helpful in practical applications. Using iterator, we may loop through every element in the unordered map.</p> <h3>Example:</h3> <pre> #include #include using namespace std; int main() { // when we will declare a umap it must be of type and here the key will be of string type and the mapped value of double in nature unordered_mapumap = { //in this we will insert the element in map directly {'one', 1}, {'two', 2}, {'three', 3} }; // here wi will insert the values by the help of the [] operator umap['the value of pi'] = 3.14; umap['the value of root2'] = 1.414; umap['the value ofroot3'] = 1.732; umap['the value oflog10'] = 2.302; umap['the value ofloge'] = 1.0; // inserting value by insert function umap.insert(make_pair('e', 2.718)); string key = 'the value of pi'; // if key not found in map iterator // to end is returned if (umap.find(key) == umap.end()) cout<< key <<' cannot retrieved '; if key found then iterator to that is returned else cout<< 'retrieved '<< << ' '; ; (umap.find(key)="=" umap.end()) <<' retrieved '; 'found <<endl; now we will iterate over all value of umap unordered_map::iterator itr; ' the entire elements : '; for (itr="umap.begin();" itr !="umap.end();" itr++) { cout<first ' <second } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Retrieved the value of pi Lambda value cannot retrieved The entire elements : E 2.718 The value ofloge 1 The value oflog10 2.302 The value of root2 1.414 The value ofroot3 1.732 The value of pi 3.14 Two 2 Three 3 One 1 </pre> <h3>Example:</h3> <pre> // It is a c++ program to find rhefreqency of it ,in this we will use of unordered_map of every word #include using namespace std; void printfrequencies(const string &str) { unordered_mapwordfreq; stringstream ss(str); string word; while (ss>> word) wordfreq[word]++; unordered_map:: iterator q; for (q = wordfreq.begin(); q != wordfreq.end(); q++) cout<< '(' <first << ', ' <second ') '; } int main() { string str="java t points questions " 'learn programs'; printfrequencies(str); return 0; < pre> <p> <strong>Output</strong> </p> <pre> (programs, 1) (learn, 1) (questions, 1) (t, 1) (points, 1) (java, 1) </pre> <hr></first></pre></'></pre></y.first<<>
Obrazloženje:
Ovaj rezultat posebno opravdava činjenicu da je nesređene karte izlazna vrijednost generira se nasumično ključ-vrijednost dok karta prikazuje vrijednost i ključ na uređen način.
Neuređen skup vs neuređena karta
Neke razlike između neuređenog skupa i neuređene karte su sljedeće:
Nesređena karta
- Samo (ključ-vrijednost) parovi se nalaze u elementima an nesređena karta .
- Koristite operatera '[]' za izdvajanje odgovarajuće vrijednosti ključa iz karte.
Neuređeni set
- Koristiti funkcija find(). , traži se element. Dakle, nema potrebe za operaterom.
Važna točka:
Na primjer, uzmite pitanje brojanja učestalosti pojedinačnih riječi. Budući da se brojevi ne mogu pohraniti u neuređeni skup (ili skup), umjesto toga moramo koristiti neuređenu kartu.
Karta nasuprot neuređene karte
Neke razlike između karte i neuređene karte su sljedeće:
Nesređena karta
- Bilo koji redoslijed može se koristiti za pohranjivanje neuređenog ključa karte.
- Implementacija neuređene karte rezultira neravnomjernom strukturom stabla, što onemogućuje zadržavanje redoslijeda unosa.
- Operacije na neuređenoj karti obično imaju o(1) vremenska složenost .
Karta
- Karta je uređeni popis različitih ključeva.
- Moguće je sačuvati redoslijed elemenata (putem specifičnog obilaska stabla) jer karta koristi uravnoteženu strukturu stabla.
- Kartografske operacije imaju o vremenska složenost (log n) .
Postupci za neuređenu kartu
Postoje brojne funkcije koje se mogu koristiti s neuređenom kartom. Oni koji su od najveće pomoći su:
- Operator =
- Operater[]
- Početak i završetak iteratora
- Prazan
- Veličina kapaciteta
- Za pregled, locirajte i prebrojite.
- Umetanje i brisanje
Potpuni popis metoda neuređene karte prikazan je u nastavku:
Na():
Ova C++ neuređena metoda karte vraća referenca na vrijednost s navedenim elementom kao ključ k .
Početi():
Pruža povratnu vrijednost koja je iterator koji pokazuje na prvi unos u nesređenom spremniku karte.
java int u nizu
Kraj():
Spremnik neuređene karte vraća an iterator koji pokazuje na mjesto nakon završnog elementa ().
Kanta():
Vraća broj spremnika u zbroju spremnika karte gdje je element sa ključ k postavlja se.
Bucket_count()
Ukupan broj spremnika neuređene karte je zbrojeno pomoću funkcije brojanja spremnika. Može se pozvati bez prosljeđivanja parametara.
Veličina kante
Daje neuređeni broj elemenata karte za svaki kanta () .
naprijed ulančavanje
Računati()
Daje neuređeni broj elemenata karte za svaki kanta () treba prebrojati broj elemenata u neuređenoj mapi s navedenim jednakim rasponom ključa.
jednak_raspon()
Vraća granice raspona sa svim stavkama spremnika i ključem s kojim se uspoređuje k .
Pronaći()
Daje iterator praznom elementu.
Pozicija ()
java liste
Određuje je li spremnik neuređenog spremnika karte prazan.
Izbriši()
Elementi u nesređenom spremniku karte mogu se izbrisati pomoću obrisati() funkcija.
Iako funkcije za prikaz interne veličine spremnika, broja spremnika, korištene hash funkcije i raznih hash pravila također osigurava c++11 biblioteka , oni su manje korisni u praktičnim primjenama. Koristeći iterator, možemo proći kroz svaki element u neuređenoj mapi.
Primjer:
#include #include using namespace std; int main() { // when we will declare a umap it must be of type and here the key will be of string type and the mapped value of double in nature unordered_mapumap = { //in this we will insert the element in map directly {'one', 1}, {'two', 2}, {'three', 3} }; // here wi will insert the values by the help of the [] operator umap['the value of pi'] = 3.14; umap['the value of root2'] = 1.414; umap['the value ofroot3'] = 1.732; umap['the value oflog10'] = 2.302; umap['the value ofloge'] = 1.0; // inserting value by insert function umap.insert(make_pair('e', 2.718)); string key = 'the value of pi'; // if key not found in map iterator // to end is returned if (umap.find(key) == umap.end()) cout<< key <<\' cannot retrieved \'; if key found then iterator to that is returned else cout<< \'retrieved \'<< << \' \'; ; (umap.find(key)="=" umap.end()) <<\' retrieved \'; \'found <<endl; now we will iterate over all value of umap unordered_map::iterator itr; \' the entire elements : \'; for (itr="umap.begin();" itr !="umap.end();" itr++) { cout<first \' <second } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Retrieved the value of pi Lambda value cannot retrieved The entire elements : E 2.718 The value ofloge 1 The value oflog10 2.302 The value of root2 1.414 The value ofroot3 1.732 The value of pi 3.14 Two 2 Three 3 One 1 </pre> <h3>Example:</h3> <pre> // It is a c++ program to find rhefreqency of it ,in this we will use of unordered_map of every word #include using namespace std; void printfrequencies(const string &str) { unordered_mapwordfreq; stringstream ss(str); string word; while (ss>> word) wordfreq[word]++; unordered_map:: iterator q; for (q = wordfreq.begin(); q != wordfreq.end(); q++) cout<< '(' <first << \', \' <second \') \'; } int main() { string str="java t points questions " \'learn programs\'; printfrequencies(str); return 0; < pre> <p> <strong>Output</strong> </p> <pre> (programs, 1) (learn, 1) (questions, 1) (t, 1) (points, 1) (java, 1) </pre> <hr></first></pre></\'>
Primjer:
// It is a c++ program to find rhefreqency of it ,in this we will use of unordered_map of every word #include using namespace std; void printfrequencies(const string &str) { unordered_mapwordfreq; stringstream ss(str); string word; while (ss>> word) wordfreq[word]++; unordered_map:: iterator q; for (q = wordfreq.begin(); q != wordfreq.end(); q++) cout<< '(' <first << \', \' <second \') \'; } int main() { string str="java t points questions " \'learn programs\'; printfrequencies(str); return 0; < pre> <p> <strong>Output</strong> </p> <pre> (programs, 1) (learn, 1) (questions, 1) (t, 1) (points, 1) (java, 1) </pre> <hr></first>
\'>