Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
119 views14 pages

Unordered Map

The document discusses the C++ unordered_map container. It describes how unordered maps are implemented using hash tables internally, allowing fast access, insertion and deletion of elements with average time complexity of O(1). It provides examples of common methods that can be used on unordered maps, such as begin(), end(), at(), bucket(), bucket_count(), bucket_size(), count(), and equal_range(). The document aims to explain the features and usage of unordered maps in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views14 pages

Unordered Map

The document discusses the C++ unordered_map container. It describes how unordered maps are implemented using hash tables internally, allowing fast access, insertion and deletion of elements with average time complexity of O(1). It provides examples of common methods that can be used on unordered maps, such as begin(), end(), at(), bucket(), bucket_count(), bucket_size(), count(), and equal_range(). The document aims to explain the features and usage of unordered maps in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

The University of Rijeka

Department of Informatics

Project:

<unordered_map> container

Andrijana Aleksovska
Erasmus student

Lipanj 2021
Spremnik <unordered_map> 2

Content
Introduction .......................................................................................................................................... 3

Argumentation ..................................................................................................................................... 4

Hash Table ........................................................................................................................................ 4

Map .................................................................................................................................................... 4

Unordered map ................................................................................................................................ 4

Unordered map container.............................................................................................................. 5

Unordered maps with usual map containers ............................................................................. 6

Unordered maps with usual map containers ............................................................................. 6

Methods on Unordered maps ....................................................................................................... 6

begin()............................................................................................................................................ 6

end() ............................................................................................................................................... 7

at() .................................................................................................................................................. 7

bucket() ......................................................................................................................................... 8

bucket_count() ............................................................................................................................. 8

bucket_size() ................................................................................................................................ 9

count() ........................................................................................................................................... 9

equal_range() ............................................................................................................................. 10

Example 1: C++ program to demonstrate the working of unordered_map ....................... 11

Example 2: C++ program to find frequencies of every word or sign using


unordered_map ............................................................................................................................. 12

Conclusion ......................................................................................................................................... 13

Sources ............................................................................................................................................... 14

Andrijana Aleksovska
Spremnik <unordered_map> 3

Introduction

Algorithm is a step-by-step procedure, which defines a set of instructions to be


executed in a certain order to get the desired output. Algorithms are generally created
independent of underlying languages, i.e. an algorithm can be implemented in more than
one programming language.
From the data structure point of view, following are some important categories of
algorithms −
 Search − Algorithm to search an item in a data structure.
 Sort − Algorithm to sort items in a certain order.
 Insert − Algorithm to insert item in a data structure.
 Update − Algorithm to update an existing item in a data structure.
 Delete − Algorithm to delete an existing item from a data structure.

In this project is explained what is unordered_map, some features and examples using
c++.

Andrijana Aleksovska
Spremnik <unordered_map> 4

Argumentation

Hash Table

Hash Table is a data structure which stores data in an associative manner. In a hash
table, data is stored in an array format, where each data value has its own unique index
value. Access of data becomes very fast if we know the index of the desired data.
Thus, it becomes a data structure in which insertion and search operations are very fast
irrespective of the size of the data. Hash Table uses an array as a storage medium and
uses hash technique to generate an index where an element is to be inserted or is to be
located from.

Map

A map, also known as an associative array is a list of elements, where each element is
a key/value pair. So, each key corresponds to a value. Different keys can have the same
value, for ordinary work. For example, the keys can be a list of fruits and the corresponding
values, the colors of the fruits. In C++, the map is implemented as a data structure with
member functions and operators. An ordered map is one where the element pairs have
been ordered by keys. An unordered map is one where there is no order.

Unordered map

C++ unordered_map containers are faster than typical map containers. The reason for
this is attributed to the fact that the unordered map is implemented using hash tables.
C++ unordered_map is a built-in container that is used to store elements in the form of
key-value pairs. In the unordered_map containers, the values are not defined in any
particular fashion internally.
The data types of both the key values and the mapped values can either be predefined
or executed at the time, and values are inserted into the container.
Internally C++ unordered_map is implemented using Hash Table, the key provided to
map is hashed into indices of a hash table that is why the performance of data structure
depends on hash function a lot but on an average, the cost of search, insert and
delete from the hash table is O(1). The worst-case complexity is O(n 2).

Andrijana Aleksovska
Spremnik <unordered_map> 5

The key values of the map are linked to the hash values of the table, which are then
organized into separate buckets. This way, once the hash values are calculated, the
compiler can quickly access the exact bucket where the specified element is located.
In more general terms, inordered map is a dictionary like data structure. It is a
sequence of (key, value) pair, where only single value is associated with each unique
key. It is often referred to as associative array. It enables fast retrieval of individual
elements based on their keys. It also implements the direct access operator(subscript
operator[]) which allows for direct access of the mapped value using its key value as
argument.
Unordered map does not sort its element in any particular order with respect to either
their key or mapped values, instead organizes into buckets depending on their hash
values to allow for fast access to individual elements directly by their key values.
Unordered map performs better than map while accessing individual elements by their
keys. But for range iteration their performance is considerably low.

Syntax:
Unordered map container

Andrijana Aleksovska
Spremnik <unordered_map> 6

Output:

Unordered maps with usual map containers

In map containers, elements are placed in proper order, whereas, in unordered maps,
the order is completely random. This is attributed to the fact that map containers are
implemented using tree structures having specific traversal paths. Still, the unordered
maps are done using the hash tables.
But this makes the unordered maps quite faster accessible with time complexity of O(1),
compared to typical map containers having O(nlog n).

Unordered maps with usual map containers

In unordered sets, the elements are not stored in key-value pairs but rather just in the
form of keys used to judge the presence of items in a set.
But with unordered maps, we can have the frequencies of the presence of an element
except for just its presence.

Methods on Unordered maps

begin()

The begin() function returns a bidirectional iterator that is pointing to the very first
element of the map. This function demands no parameter to be passed and shows an
error when it is done.

Syntax:
Example:

Andrijana Aleksovska
Spremnik <unordered_map> 7

Returns an iterator pointing to the first element of the map.

end()

The end() function is meant to return a bidirectional iterator pointing to the element next
to the last element in the map container.
Just like begin() function, this too doesn’t require any parameter. Also, since it is pointing
to a non-valid element, it cannot be dereferenced.
Syntax:
Example:

Returns an iterator pointing to next of the last element of the map.

at()

The at() function is used to access the mapped values in the unordered map container
whose key values you know.
Syntax:

Andrijana Aleksovska
Spremnik <unordered_map> 8

Example:

Output:

bucket()

The bucket() function is used to get the bucket number, which holds the mapped value
of the key-value you provide as a parameter.
Syntax:
Example:

Output:

bucket_count()

The bucket_count() function is used to get the total number of buckets in an unordered
map container. This function doesn’t require any parameters.
Syntax:

Andrijana Aleksovska
Spremnik <unordered_map> 9

Example:

Output:

bucket_size()

The bucket_size() function is used to get the number of elements in a single bucket of
the unordered map container.
Syntax:
Example:

Output:

count()

The count() function is used to get several elements with the same key value in a
container. But since a map container allows only one mapped value with a certain key
value, this function only checks if a key is present or not.
Syntax:

Andrijana Aleksovska
Spremnik <unordered_map> 10

Example:

Output:

equal_range()

The equal_range() function returns the initial and ends iterators of the range K, which
contains all the elements whose key is equal to K. Now, since, in map containers, there
are no duplicate keys, the range contains at most one value.

Syntax:
Example:

Output:

Andrijana Aleksovska
Spremnik <unordered_map> 11

Example 1: C++ program to demonstrate the working of unordered_map

Output:

Andrijana Aleksovska
Spremnik <unordered_map> 12

Example 2: C++ program to find frequencies of every word or sign using


unordered_map

Output:

Andrijana Aleksovska
Spremnik <unordered_map> 13

Conclusion

A map, also known as an associative array is a list of elements, where each element is
a key/value pair. So, each key corresponds to a value. In C++, the map is implemented
as a data structure with member functions and operators. An ordered map is one where
the element pairs have been ordered by keys. An unordered map is one where there is
no ordering.
Technically, a hash consists of pair<type1, type2> elements. In fact, the pair is a whole
data structure with its member functions and operators. The two template parameters for
the pair are the same two template parameters for the unordered_map.
The initializer_list for the map is an array literal of literals. Each internal literal consists
of two objects, the key/value pair.
The member functions and operators for unordered_map can be categorized under the
following headings: unordered_map construction/copy constructing, unordered_map
Capacity, unordered_map iterator, unordered_map Operations, and unordered_map
Modifiers.
An unordered map is used when a key has to be mapped to a value.

Andrijana Aleksovska
Spremnik <unordered_map> 14

Sources

 Data Structures and Algorithm Analysis in C++ - Mark Allen Weiss


 Programirane=++Algoritmi – Preslav Nakov, Panajot Dobrikov
 The C++ Programming Language – Bjarne Stroustrup
 C/C++ Programer’s Refrence – Herbert Shildt
 unordered_map in C++
 unordered_map in C++
 How to use unordered_map efficiently in C++
 Everything about unordered_map

Andrijana Aleksovska

You might also like