Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Advanced
Apr 16, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using an Associative Array

An associative array (also called map or dictionary) is an array for which the index does not have to be an integer. An associative array stores pairs of values. One serves as the key and the other is the associated value. The map<> container is an example of an associated array. In this example, a map is used to translate the string value of an enumerator into its corresponding integral value. The string is the key whose associated value is an int:
#include <map>   
#include <string>
#include <iostream>
using namespace std;
enum directions {up, down};

int main() 
  pair<string, int> Enumerator(string("down"), down); //create a pair
  map<string, int> mi; //create a map
  mi.insert(Enumerator); //insert the pair element
  int n = mi["down"]; //access the associated value of "down"; n = 1
The Standard Template Library (STL) defines the template pair<class Key, class Value> that serves as a map element.
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date