Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

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.

 

 

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