Using an Associative Array

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    #include #include using namespace std;enum directions {up, down};int main() {  pair Enumerator(string("down"), down); //create a pair  map 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 that serves as a map element.

See also  10 Best Apps to Help You Read More Books in Less Time

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist