Javatpoint Logo
Javatpoint Logo

Associative arrays in C++

In this article, we will discuss Associative arrays in C++ with their features and examples.

In C++ language, Associative arrays will refer to data structures that associate keys and values. These are efficient for storing and retrieving values based on their corresponding keys. These associative arrays are implemented using various containers provided by Standard Template Library. The primary containers for associative arrays are map and unordered_map. These containers allow us to associate keys with values. These are versatile and powerful data structures that can be used to solve a variety of problems.

Essential features of the Associative arrays in C++:

There are several features of the associative arrays in C++. Some main features of the associative arrays are as follows:

  • Key-value Association:

Each element in an associative array must only be present in a key-value pair. Where the keys are unique, each value is related to only one key. The value of the data is associated with the key.

  • Flexible key data types:

Keys in the associative arrays can be of any kind, including integers, strings, objects or user-defined types.

  • Efficient key-based lookup:

These are optimized for the efficient lookup of elements based on their keys. Accessing a value using a key is faster than iterating through a traditional array.

  • Dynamic size and Insertion:

Associative arrays can grow when a new key-value pair is added. They shrink whenever we delete or remove a key-value pair from it, so there will be no problem when dealing with the size of the arrays.

Common operations in Associative Arrays:

There are several common operations in associative arrays. Some main operations in the associative arrays are as follows:

  • Insertion:

This operation adds a new key-value pair to the associative arrays. It is done using the square brackets or the insert function in C++.

  • Accessing Elements:

Accessing retrieves the value associated with a specific key. If the key is present in the array, we can access its value by using it. A new entry is created with a default constructed value if the key is not current.

  • Updating Elements:

Updating allows the modification of the value associated with a specific key. It will create a new key-value pair if it is not present in the array.

  • Deletion:

This operation will remove a particular key-value pair in the associative array. The erase function is commonly used for this operation.

  • Checking Existence:

The find function checks whether a particular key exists in the associative array. It returns the iterator pointer to the element if the key is present in the array. It returns end() if the key is not current.

  • Size:

The size function is present to know the size of the associative array

These are the fundamental operations for manipulating and querying associative arrays in C++. These associative arrays will offer faster average lookup times. The time complexities associated with these operations, especially when dealing with large datasets.

Applications of associative arrays in C++:

  • These are used in various scenarios where efficient key-based data retrieval is vital. These are used in dictionaries, database indexing, symbol tables, configuration management, caching, counting occurrences, graph algorithms, task scheduling, memorization, frequency analysis, http service, and configuration management.
  • In caching systems, associative arrays can store frequently accessed data with unique keys, enabling fast retrieval and reducing the need to fetch data from a slower source.
  • When a programmer deals with graph algorithms, associative arrays represent adjacency lists, mapping each vertex to its list of neighbouring vertices.
  • When a programmer is solving a program related to dynamic programming and recursion, associative arrays can be used for memorization, which stores the previously computed results to avoid false computations.
  • In web development, associative arrays can represent HTTP headers, where the header names are keys, and the corresponding values are the header values.

Now, we will discuss about some of the associative arrays:

  • Map:

These are implemented as sorted binary trees and maintain a sorted order of keys. It is most commonly used in programming.

  • Unordered_map:

It implements a hash table. It will provide faster insertion and retravel than a map. Here, the order of insertion of the key-value pair is not maintained.

  • Mulimap:

It is also similar to a map, but it will allow duplicate key pairs. It means that it will enable multiple elements with the same key.

  • Unordered_multimap:

It is similar to unordered_map and is implemented by using a hash table. It allows multiple elements with the same key.

Example:

Let us take a C++ program to illustrate the unordered_map:

Output:

Associative arrays in C++

Example 2:

Let us take a C++ program to illustrate the map:

Output:

Associative arrays in C++

Example 3:

Let us take a C++ program to illustrate multimap:

Output:

Associative arrays in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA