RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

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