logo

UNORDERED_MAP ב-C++

ה מפה לא מסודרת הוא מיכל משויך שמכיל אלמנטים שנוצרו על ידי מיזוג ערך ממופה עם ערך מפתח. האלמנט מזוהה באופן ספציפי על ידיו ערך מפתח , וה ערך ממופה הוא התוכן הקשור למפתח. המפתחות והערכים יכולים להיות שניהם של כל מבוסס או סוג מוגדר על ידי המשתמש . מפה לא מסודרת יכולה להיחשב כמבנה נתונים מסוג מילון המאחסן אלמנטים בתוך עצמו. הזוגות הרציפים שהוא מחזיק (ערך מפתח) לאפשר שליפה מהירה של אלמנט ספציפי באמצעות המפתח האישי שלו.

המפתח שסופק למפה הוא hashed לתוך המדדים של טבלת גיבוב, וזו הסיבה שמהירות מבנה הנתונים תלויה במידה רבה בפונקציית ה-hash, אך בממוצע, העלות של חפש, הוסף ומחק מטבלת האש הוא o(1).

במקרה הגרוע ביותר, במיוחד עבור מספרים שלמים ראשוניים גדולים, זה מורכבות הזמן יכול לנוע מ o(1) ל עַל) . מומלץ מאוד להשתמש במפה במקרה זה כדי להימנע מקבלת טל (חרגת ממגבלת הזמן) נושא.

java do while לדוגמה

תחביר:

 Unordered_mapumap 

דוגמא:

 //A c++ program to check an unordered map in it. #include #include using namespace std; int main() { unordered_mapumap; umap[&apos;javatpoint&apos;] = 20; umap[&apos;regular&apos;] = 30; umap[&apos;distribute&apos;] = 40; for (auto y :umap) cout&lt;<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&apos;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>&apos;[]&apos;</strong> to extract a key&apos;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&apos; 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&apos;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&apos;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&apos;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&apos;s element count for each <strong> <em>bucket ()</em> .</strong> </p> <p> <strong>Count()</strong> </p> <p>It gives the unordered map count&apos;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&apos;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&apos;s empty.</p> <p> <strong>Position ()</strong> </p> <p>It determines whether the unordered map container&apos;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 {&apos;one&apos;, 1}, {&apos;two&apos;, 2}, {&apos;three&apos;, 3} }; // here wi will insert the values by the help of the [] operator umap[&apos;the value of pi&apos;] = 3.14; umap[&apos;the value of root2&apos;] = 1.414; umap[&apos;the value ofroot3&apos;] = 1.732; umap[&apos;the value oflog10&apos;] = 2.302; umap[&apos;the value ofloge&apos;] = 1.0; // inserting value by insert function umap.insert(make_pair(&apos;e&apos;, 2.718)); string key = &apos;the value of pi&apos;; // if key not found in map iterator // to end is returned if (umap.find(key) == umap.end()) cout&lt;&lt; key &lt;<' 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 &amp;str) { unordered_mapwordfreq; stringstream ss(str); string word; while (ss&gt;&gt; word) wordfreq[word]++; unordered_map:: iterator q; for (q = wordfreq.begin(); q != wordfreq.end(); q++) cout&lt;&lt; &apos;(&apos; <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<<>

הֶסבֵּר:

פלט זה מצדיק במיוחד את העובדה שה מפות לא מסודרות ערך הפלט נוצר באקראי מפתח לערך תוך כדי שהמפה מציגה ערך ומפתח בצורה מסודרת.

סט לא מסודר לעומת מפה לא מסודרת

כמה הבדלים בין סט לא מסודר למפה לא מסודרת הם כדלקמן:

מפה לא מסודרת

  • רק (ערך מפתח) זוגות נמצאים באלמנטים של an מפה לא מסודרת .
  • השתמש במפעיל '[]' כדי לחלץ את הערך המתאים של מפתח ממפה.

סט לא מסודר

    ערך מפתח זוגות משמשים בעיקר כדי לקבוע אם קבוצה קיימת או נעדרת ולא תמיד נוכחים בקבוצה לא מסודרת.
  • משתמש ב פונקציה find(). , מחפשים אלמנט. לפיכך, אין צורך במפעיל.

נקודה חשובה:

לדוגמה, קחו את הנושא של ספירת התדירות של מילים בודדות. מכיוון שלא ניתן לאחסן ספירות ב סט לא מסודר (או סט), במקום זאת עלינו להשתמש במפה לא מסודרת.

מפה לעומת מפה לא מסודרת

כמה הבדלים בין המפה למפה לא מסודרת הם כדלקמן:

מפה לא מסודרת

  • ניתן להשתמש בכל הזמנה לאחסון מפתח המפה הלא מסודר.
  • יישום מפה לא מסודרת מביא למבנה עץ לא אחיד, מה שלא מאפשר לשמור על סדר הערכים.
  • לפעולות על מפה לא מסודרת יש בדרך כלל o(1) מורכבות זמן .

מַפָּה

  • המפה היא רשימה מסודרת של מפתחות נפרדים.
  • אפשר לשמר את סדר האלמנטים (על ידי חציית עצים ספציפית) מכיוון שמפה משתמשת במבנה עץ מאוזן.
  • לפעולות המפה יש o מורכבות זמן (לוג n) .

נהלים למפה לא מסודרת

ישנן פונקציות רבות שניתן להשתמש בהן עם מפה לא מסודרת. אלה שהכי מועילים הם:

  • מפעיל =
  • מַפעִיל[]
  • התחלה וסיום של האיטרטור
  • ריק
  • גודל הקיבולת
  • לחיפוש, אתר וספר.
  • הוסף ומחק

הרשימה המלאה של שיטות של מפה לא מסודרת מוצגת להלן:

בְּ():

שיטת מפה לא מסודרת של c++ זו החזרות הפניה לערך עם האלמנט שצוין בתור מפתח ק .

התחל():

הוא מספק ערך החזר שהוא an מצביע איטרטור לערך הראשון במיכל המפה הלא מסודר.

סוֹף():

דלי מיכל המפה הלא מסודר מחזיר מצביע איטרטור למיקום שאחרי האלמנט הסופי ().

דְלִי():

הוא מחזיר את מספר הדלי בספירת הדליים של המפה היכן שהרכיב עם מפתח ק ממוקם.

bucket_count()

המספר הכולל של דליים של המפה הלא מסודרת הוא נאמד באמצעות פונקציית ספירת הדליים. ניתן לקרוא לו מבלי להעביר פרמטרים כלשהם.

גודל הדלי

זה נותן את ספירת האלמנטים של ספירת המפה הלא מסודרת עבור כל אחד מהם דלי () .

לספור()

זה נותן את ספירת האלמנטים של ספירת המפה הלא מסודרת עבור כל אחד מהם דלי () יש לספור את מספר האלמנטים במפה לא מסודרת עם טווח המפתח השווה שצוין.

equal_range()

זה מחזיר את הגבולות של טווח עם כל פריטי המכולה ומפתח שמשתווה אליו ק .

למצוא()

נותן איטרטור לריק של האלמנט.

עמדה ()

הוא קובע אם המיכל של מיכל המפה הלא מסודר ריק.

לִמְחוֹק()

ניתן למחוק אלמנטים במיכל המפה הלא מסודר באמצעות ה- לִמְחוֹק() פוּנקצִיָה.

למרות שהפונקציות להצגת גודל הדלי הפנימי, ספירת הדליים, פונקציית ה-hash בשימוש ומדיניות גיבוב שונים מסופקות גם על ידי ספריית c++11 , הם פחות מועילים ביישומים מעשיים. באמצעות איטרטור, אנו עשויים בלולאה דרך כל אלמנט במפה הלא מסודרת.

דוגמא:

 #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 {&apos;one&apos;, 1}, {&apos;two&apos;, 2}, {&apos;three&apos;, 3} }; // here wi will insert the values by the help of the [] operator umap[&apos;the value of pi&apos;] = 3.14; umap[&apos;the value of root2&apos;] = 1.414; umap[&apos;the value ofroot3&apos;] = 1.732; umap[&apos;the value oflog10&apos;] = 2.302; umap[&apos;the value ofloge&apos;] = 1.0; // inserting value by insert function umap.insert(make_pair(&apos;e&apos;, 2.718)); string key = &apos;the value of pi&apos;; // if key not found in map iterator // to end is returned if (umap.find(key) == umap.end()) cout&lt;&lt; key &lt;<\' 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 &amp;str) { unordered_mapwordfreq; stringstream ss(str); string word; while (ss&gt;&gt; word) wordfreq[word]++; unordered_map:: iterator q; for (q = wordfreq.begin(); q != wordfreq.end(); q++) cout&lt;&lt; &apos;(&apos; <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></\'>

דוגמא:

 // 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 &amp;str) { unordered_mapwordfreq; stringstream ss(str); string word; while (ss&gt;&gt; word) wordfreq[word]++; unordered_map:: iterator q; for (q = wordfreq.begin(); q != wordfreq.end(); q++) cout&lt;&lt; &apos;(&apos; <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>