Associative arrays in C++

In this tutorial, we will learn about associative arrays in C++. Generally, an array represents a collection of elements having similar data types accessed by an index number which starts from 0 to upwards. But, we cannot provide any other unique index naming to access the data elements. So, the concept of associative arrays in C++ removes this drawback. You will learn everything about associative arrays along with implementation using a C++ program in this tutorial.

Associative arrays in C++ with example

In C++, an associative array is a special type of array in which the index can be of any data type. The index can be an integer, character, float, string, etc. But, the index values must be unique for accessing data elements in the array. The index in an associative array is called as key and the data stored at that position is called as value. Thus, we can define the associative array as a key-value pair. So, an associative array consists of (key, value) pairs. Associative arrays are also known as maps or dictionaries in C++.

arr_name[“key_name”]=value;

For example – Fruit[“strawberry”]=200
Let’s see examples of key-value pairs.

Fruits        Price

Apple         120
Guava         40
Lime          20
Banana        30
Peach         50
Strawberry    200
Blueberry     130

In the above example, the name of fruits is the key and price is the value for defining an associative array. This will simplify the manipulation of data. We can easily access the price of different fruits by their names.

Program in C++ demonstrating an associative array

To illustrate associative arrays, consider the example of fruits and their price. In the program, the key is the name of fruit and value is its price. The C++ program for associative arrays is –

#include <iostream>
#include <vector>
using namespace std;
class Fruits
{
  typedef struct Record
  {
          int price;
          string name;
  }Fruit_data;
  vector<Fruit_data> stack;

  public:
  bool Fruit_present(string fruit_name)
  {
    for(int i=0;i<stack.size();++i)
    {
      if(stack[i].name==fruit_name)
        return true;
    }
    return false;
  }
  void Add_fruit()
  {
    string fruit_name;
    int fruit_price;
    cout<<"\nENTER FRUIT NAME : ";
    cin>>fruit_name;
    cout<<"ENTER PRICE : ";
    cin>>fruit_price;
    if(Fruit_present(fruit_name))
    {
      cout<<"\n--FRUIT ALREADY EXIST--";
    }
    else
    {
      Fruit_data item;
      item.name=fruit_name;
      item.price=fruit_price;
      stack.push_back(item);
      cout<<"\n--FRUIT ADDED--";
    }
  }
  int& operator [] (string fruit_name)
  {
    for(int i=0; i<stack.size(); i++)
    {
      if(stack[i].name==fruit_name)
        return stack[i].price;
    }
    long index = stack.size();
    Fruit_data item;
    item.name = fruit_name;
    stack.push_back(item);
    return stack[index].price;
  }
  void display()
  {
    for(int i=0;i<stack.size();++i)
    {
      cout<<"\n\nFRUIT "<<i+1;
      cout<<"\nFRUIT NAME : "<<stack[i].name;
      cout<<"\nFRUIT PRICE : Rs."<<stack[i].price;
    }
  }
};
int main()
{
  Fruits array;
  string name;

  array.Add_fruit();
  array.Add_fruit();
  array["apple"]=120;
  array["strawberry"]=200;

  cout<<"\nENTER FRUIT NAME TO SEARCH : ";
  cin>>name;
  if(array.Fruit_present(name))
    cout<<"THE PRICE OF FRUIT "<<name<<" IS : Rs."<<array[name];
  else
    cout<<"FRUIT NOT FOUND";
  array.display();
  return 0;
}

In the program, we create a C++ class named Fruits containing a vector stack of data type Fruit_data. The stack vector stores n number of fruit-price values. The function Fruit_present() checks if the fruit is present in the array or not. The function Add_fruit() takes input from the user and adds fruit to the array. To enable adding an element to the array using the subscript operator, we define a subscript operator overloading function. The display() function displays the data stored in the associative array.

C++ program output

The output of the C++ program to illustrate associative arrays is –

[email protected]:~/cpp$ g++ aa.cpp
[email protected]:~/cpp$ ./a.out

ENTER FRUIT NAME : banana
ENTER PRICE : 40

--FRUIT ADDED--
ENTER FRUIT NAME : apple
ENTER PRICE : 100

--FRUIT ADDED--
ENTER FRUIT NAME TO SEARCH : guava
FRUIT NOT FOUND

FRUIT 1
FRUIT NAME : banana
FRUIT PRICE : Rs.40

FRUIT 2
FRUIT NAME : apple
FRUIT PRICE : Rs.120

FRUIT 3
FRUIT NAME : strawberry
FRUIT PRICE : Rs.200
[email protected]:~/cpp$

So, the array indexes are –

{banana, apple, strawberry}

And the values are –

{40, 120, 200}

Thank you for reading this tutorial. I hope it helped you a lot.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *