UNORDERED_MAP IN C++The unordered map is an associated container that holds elements created by fusing a mapped value with a key value. The element is identified specifically by its key value, and the mapped value is the content related to the key. Keys and values may both be of any established or user-defined type. An unordered map can be thought of as a data structure of the dictionary type that stores elements within itself. The sequential pairs it holds (key, value) enable quick retrieval of a specific element using its individual key. The key supplied to the map is hashed into the indices of a hash table, which is why the speed of the data structure heavily depends on the hash function, but on average, the cost of search, insert, and delete from the hash table is o(1). In the worst case, especially for large prime integers, its time complexity can range from o(1) to o(n). It is highly recommended to utilise a map in this case to avoid receiving a tle (time limit exceeded) issue. Syntax:Example:Output Distribute 40 Regular 30 Javatpoint 20 Explanation: This output specifically justifies the fact that the unordered map's output value is generated in a random key-to-value manner while the map shows value and key in an ordered fashion. Unordered set vs Unordered mapSome differences between Unordered set and Unordered map are as follows: Unordered map
Unordered set
Important point: For instance, take the issue of counting the frequency of individual words. Since, counts cannot be stored in unordered set (or set), we must instead use unordered map. Map vs. Unordered mapSome differences between the Map and Unordered map are as follows: Unordered map
Map
Procedures for unordered mapThere are numerous functions that can be used with unordered map. The ones who are most helpful are:
The full list of an unordered map's methods is shown below: At(): This c++ unordered map method returns a reference to the value with the specified element as the key k. Begin(): It provides a return value that is an iterator pointing to the first entry in the unordered map container. End(): The unordered map container bucket returns an iterator pointing to the location after the final element (). Bucket(): It returns the bucket number in the map's bucket count where the element with key k is placed. Bucket_count() The unordered map's total number of buckets is tallied using the bucket count function. It can be called without passing any parameters. Bucket size It gives the unordered map count's element count for each bucket (). Count() It gives the unordered map count's element count for each bucket () the number of elements in an unordered map with the specified key equal range should be counted. Equal_eange() It returns the boundaries of a range with all the container's items and a key that compares to k. Find() Gives an iterator to the element's empty. Position () It determines whether the unordered map container's container is empty. Erase() Elements in the unordered map container can be deleted using the erase() function. Although the functions to view the internal bucket size, bucket count, used hash function, and various hash policies are also provided by the c++11 library, they are less helpful in practical applications. Using iterator, we may loop through every element in the unordered map. Example:Output 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 Example:Output (programs, 1) (learn, 1) (questions, 1) (t, 1) (points, 1) (java, 1) Next TopicC++ Books for Beginners |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India