AppDividend
Latest Code Tutorials

C++ Unordered_map Example | Unordered_map In C++ Tutorial

0

C++ Unordered_map Example | Unordered_map In C++ Tutorial is today’s topic. Unordered map containers are used to store elements in the form of key-value pairs. 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. In unordered map containers, the values are not defined in any particular fashion internally.

C++ Unordered_map Example

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. 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.

#Syntax

unordered_map<key_datatype, mapped_datatype> map_name;

#Program to show the creation of Unordered map container

#include <iostream>
#include <iterator>
#include <unordered_map>
using namespace std;
int main()
{
    unordered_map<int, char> map1;
    unordered_map<int, char>::iterator cursor;
    map1[1] = 'a';
    map1[2] = 'b';
    cout << "KEY\tELEMENT" << endl;
    for (cursor = map1.begin(); cursor != map1.end(); cursor++)
    {
        cout << cursor->first;
        cout << '\t' << cursor->second << '\n'
             << endl;
    }
}

See the following output.

 

C++ Unordered_map Example

Comparison of Unordered maps :

#With usual map containers

In map containers, elements are placed in a proper order whereas in unordered maps the order is completely random. The reason for this is attributed to the fact that map containers are implemented using tree structure having specific traversal paths, but the unordered maps are done using the hash tables. But this makes the unordered maps quite faster accessible with time complexity of O(1), as compared to typical map containers having O(nlogn).

#With unordered sets

In unordered sets, the elements are not stored in key-value pairs but rather just in the form of keys which are used to judge the presence of elements 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:

mapcontainer_name.begin();

See the following program.

#include <iostream>
#include <iterator>
#include <map>
using namespace std;
int main()
{
  map<int, char> map1;
  map<int, char>::iterator cursor;
  map1[1] = 'a';
  map1[2] = 'b';
  map1.begin();
}

(Returns an iterator pointing the first element of the map.)

#end()

The end() function is meant to return a bidirectional iterator that is 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:

mapcontainer_name.end();

See the following program.

#include <iostream>
#include <iterator>
#include <map>
using namespace std;
int main()
{
  map<int, char> map1;
  map<int, char>::iterator cursor;
  map1[1] = 'a';
  map1[2] = 'b';
  map1.end();
}

(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:

mapcontainer_name.at(key_value);

See the following program.

#include <iostream>
#include <iterator>
#include <unordered_map>
using namespace std;
int main()
{
  unordered_map<int, char> map1;
  unordered_map<int, char>::iterator cursor;
  map1[1] = 'a';
  map1[2] = 'b';
  cout << map1.at(2) << endl;
}

See the following output.

 

Syntax

#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:

mapcontainer_name.bucket(key_value);

See the following program.

#include <iostream>
#include <iterator>
#include <unordered_map>
using namespace std;
int main()
{
  unordered_map<int, char> map1;
  unordered_map<int, char>::iterator cursor;
  map1[1] = 'a';
  map1[2] = 'b';
  cout << map1.bucket(2) << endl;
}

See the following output.

 

obucket

#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:

mapcontainer_name.bucket(key_value);

See the following program.

#include <iostream>
#include <iterator>
#include <unordered_map>
using namespace std;
int main()
{
  unordered_map<int, char> map1;
  unordered_map<int, char>::iterator cursor;
  map1[1] = 'a';
  map1[2] = 'b';
  cout << map1.bucket_count() << endl;
}

See the following output.

 

ibucket_count

#bucket_size()

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

#Syntax

mapcontainer_name.bucket_size(bucket_value);

See the following program.

#include <iostream>
#include <iterator>
#include <unordered_map>
using namespace std;
int main()
{
  unordered_map<int, char> map1;
  unordered_map<int, char>::iterator cursor;
  map1[1] = 'a';
  map1[2] = 'b';
  cout << map1.bucket_size(2) << endl;
}

See the following output.

 

obucket_size

#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:

mapcontainer_name.count(key_value);

See the following program.

#include <iostream>
#include <iterator>
#include <unordered_map>
using namespace std;
int main()
{
  unordered_map<int, char> map1;
  unordered_map<int, char>::iterator cursor;
  map1[1] = 'a';
  map1[2] = 'b';
  cout << map1.count(1) << endl;
}

See the following output.

 

ocount

#equal_range()

The equal_range() function returns the initial and end iterators of the range K which contain 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:

mapcontainer_name.equal_range(key_value);

See the following program.

#include <iostream>
#include <iterator>
#include <unordered_map>
using namespace std;
int main()
{
    unordered_map<int, char> map1;
    unordered_map<int, char>::iterator cursor;
    map1[1] = 'a';
    map1[2] = 'b';
    auto len = map1.equal_range(1);
    for (auto m = len.first; m != len.second; m++)
    {
        cout << "Key : " << m->first << endl;
        cout << "Value : " << m->second << endl;
    }
}

See the following output.

 

oequal_range

Finally, C++ unordered_map Example Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.