Open In App

Associative arrays in C++

Last Updated : 09 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Associative arrays are also called map or dictionaries. In C++. These are special kind of arrays, where indexing can be numeric or any other data type 
i.e can be numeric 0, 1, 2, 3.. OR character a, b, c, d… OR string geek, computers… 
These indexes are referred as keys and data stored at that position is called 
value
So in associative array we have (key, value) pair. 
We use STL maps to implement the concept of associative arrays in C++.
 

Example
Now we have to print the marks of computer geeks whose names and marks are as follows 
 

  Name        Marks
       Jessie       100
       Suraj        91
       Praveen      99   
       Bisa         78
       Rithvik      84

 

CPP




// CPP program to demonstrate associative arrays
#include <bits/stdc++.h>
using namespace std;
int main()
{
    // the first data type i.e string represents
    // the type of key we want the second data type
    // i.e int represents the type of values we
    // want to store at that location
    map<string, int> marks{ { "Rithvik", 78 },
            { "Suraj", 91 }, { "Jessie", 100 },
            { "Praveen", 99 }, { "Bisa", 84 } };
 
     map<string, int>::iterator i;
    cout << "The marks of all students are" << endl;
    for (i = marks.begin(); i != marks.end(); i++)
        cout << i->second << " ";
     
 
    cout << endl;
 
    // the marks of the students based on there names.
    cout << "the marks of Computer geek Jessie are"
         << " " << marks["Jessie"] << endl;
 
    cout << "the marks of geeksforgeeks contributor"
          " Praveen are " << marks["Praveen"] << endl;
}


Output: 

The marks of all students are
84 100 99 78 91 
the marks of Computer geek Jessie are 100
the marks of geeksforgeeks 
 Praveen are 99

 


Similar Reads

Sequence vs Associative containers in C++
Sequence Containers In standard template library they refer to the group of container class template, we use to them store data. One common property as the name suggests is that elements can be accessed sequentially. Each of the following containers use different algorithm for data storage thus for different operations they have different speed. An
3 min read
Ways to multiply n elements with an associative operation
Given a number n, find the number of ways to multiply n elements with an associative operation. Examples : Input : 2 Output : 2 For a and b there are two ways to multiply them. 1. (a * b) 2. (b * a) Input : 3 Output : 12 Explanation(Example 2) : For a, b and c there are 12 ways to multiply them. 1. ((a * b) * c) 2. (a * (b * c)) 3. ((a * c) * b) 4.
8 min read
Maximum OR sum of sub-arrays of two different arrays
Given two arrays of positive integers. Select two sub-arrays of equal size from each array and calculate maximum possible OR sum of the two sub-arrays. Note: Let f(x, l, r) is the OR sum of all the elements in the range [l, r] in array x. Examples : Input : A[] = {1, 2, 4, 3, 2} B[] = {2, 3, 3, 12, 1} Output : 22 Explanation: Here, one way to get m
5 min read
Merge k sorted arrays | Set 2 (Different Sized Arrays)
Given k sorted arrays of possibly different sizes, merge them and print the sorted output.Examples: Input: k = 3 arr[][] = { {1, 3}, {2, 4, 6}, {0, 9, 10, 11}} ;Output: 0 1 2 3 4 6 9 10 11 Input: k = 2 arr[][] = { {1, 3, 20}, {2, 4, 6}} ;Output: 1 2 3 4 6 20 We have discussed a solution that works for all arrays of the same size in Merge k sorted a
7 min read
Minimize sum of product of same-indexed elements of two arrays by reversing a subarray of one of the two arrays
Given two equal-length arrays A[] and B[], consisting only of positive integers, the task is to reverse any subarray of the first array such that sum of the product of same-indexed elements of the two arrays, i.e. (A[i] * B[i]) is minimum. Examples: Input: N = 4, A[] = {2, 3, 1, 5}, B[] = {8, 2, 4, 3} Output: A[] = 1 3 2 5 B[] = 8 2 4 3 Minimum pro
12 min read
Check if two arrays can be made equal by swapping pairs of one of the arrays
Given two binary arrays arr1[] and arr2[] of the same size, the task is to make both the arrays equal by swapping pairs of arr1[ ] only if arr1[i] = 0 and arr1[j] = 1 (0 ? i &lt; j &lt; N)). If it is possible to make both the arrays equal, print "Yes". Otherwise, print "No". Examples: Input: arr1[] = {0, 0, 1, 1}, arr2[] = {1, 1, 0, 0}Output: YesEx
11 min read
Find sub-arrays from given two arrays such that they have equal sum
Given two arrays A[] and B[] of equal sizes i.e. N containing integers from 1 to N. The task is to find sub-arrays from the given arrays such that they have equal sum. Print the indices of such sub-arrays. If no such sub-arrays are possible then print -1.Examples: Input: A[] = {1, 2, 3, 4, 5}, B[] = {6, 2, 1, 5, 4} Output: Indices in array 1 : 0, 1
15+ min read
Count of possible arrays from prefix-sum and suffix-sum arrays
Given 2*N integers which are elements of a prefix and suffix array(in shuffled order) of an array of size N, the task is to find the no of possible array's of the size N which can be made from these elements Examples: Input: arr[] = {5, 2, 3, 5} Output: 2 Explanation: 1st array can be : {2, 3} Its prefix array:{2, 5} Its suffix array:{5, 3} 2nd arr
15+ min read
Difference between Static Arrays and Dynamic Arrays
In the Data structure, we know that an array plays a vital role in having similar types of elements arranged in a contiguous manner with the same data type. According to the concept of array, an array can be defined in two ways as per the memory allocation concept. Types of Arrays:There are basically two types of arrays: Static Array: In this type
5 min read
Generate all possible sorted arrays from alternate elements of two given sorted arrays
Given two sorted arrays A and B, generate all possible arrays such that the first element is taken from A then from B then from A, and so on in increasing order till the arrays are exhausted. The generated arrays should end with an element from B. Example: A = {10, 15, 25} B = {1, 5, 20, 30} The resulting arrays are: 10 20 10 20 25 30 10 30 15 20 1
12 min read
Practice Tags :