Associative Arrays  

  • Associative array:

    • is not stored as a traditional array

    • is called an "array" because usage resembles that of an array (i.e.: A[i])

  • What is an Associative array:

    • maps keys to their corresponding values

      Example:

        Name (key)      Phone (value)
        ---------       --------
        john            123-4567       (A["john"] = "123-4567")
        mary            333-3333       (A["mary"] = "333-3333")
      

  Associative array in C++: map  

  • Associative array in C++ is called: map

  • Defining a map:

      #include <map>
    
      map<string, string> map1;
      // Defines an empty map of string -> string
    
      map<string, string> map2 = { {"john", "123-4567"},
                                   {"mary", "333-3333"},
                                   {"jack", "345-6666"},
                                   {"amos", "222-8888"},
                                   {"jane", "444-5555"} }
      
      // Defines a map of int with initial mapping:
      //      "john" -> "123-4567"
      //      "mary" -> "333-3333"}
      //      "jack" -> "345-6666"}
      //      "amos" -> "222-8888"}
      //      "jane" -> "444-5555"}
    

  Associative array in C++: map  

  • Accessing values stored in a map:

      map2["john"]       // Access the value associated with "john"
      map2["mary"]       // Access the value associated with "mary"
    

  • Accessing a map using a for-loop:

      for (auto  data: map2 )
         cout << data.first << "  " << data.second << endl;
     
      // first  is the key
      // second is the value corresponding to the key
      // data is a help variable		

  Associative array in C++: map  

  • Inserting values into a map:

      map2["new"] = "999-9999";    // Inserts new -> 999-9999 to map
    

  • Deleting values from a map:

      map2.erase("new");    // Removes the key new and its value from map
    

  • Updating values stored a map:

      map2["amos"] = "000-0000;  // Updates the value in map2["amos"]
    

Demo program: map.cc

  Associative array in Python: dict (dictionary)  

  • Associative array in Python is called: dict (= dictionary)

  • Defining a dictionary:

      dict1 = []
      # Defines an empty dict
    
      dict2 = { "john" : "123-4567",
                "mary" : "333-3333",
                "jack" : "345-6666",
                "amos" : "222-8888",
                "jane" : "444-5555"  }
    
      # Defines a map of int with initial mapping:
      #      "john" -> "123-4567"
      #      "mary" -> "333-3333"}
      #      "jack" -> "345-6666"}
      #      "amos" -> "222-8888"}
      #      "jane" -> "444-5555"} 
    

  Associative array in Python: dict (dictionary)  

  • Accessing values stored in a map:

      map2["john"]       // Access the value associated with "john"
      map2["mary"]       // Access the value associated with "mary"
    

  • Accessing a map using a for-loop:

     for k in dict2:
        print( k, dict2[k] )
    
     # I used "k" to denote "key"
     # dict[k] is the value asscoiate with the key k
    

  Associative array in Python: dict (dictionary)  

  • Inserting values into a dict:

     dict2["new"] = "999-9999";    # Inserts new -> 999-9999 to dict2
    

  • Deleting values from a dict:

      del dict2["new"];    // Removes the key new and its value from dict2
    

  • Updating values stored a dict:

      dict2["amos"] = "000-0000;  // Updates the value in dict2["amos"]
    

Demo program: dict.py