Background: In C ++ Standard Library, Map containers use pair (Key/Value) to store elements, and repeated elements are not allowed. If a Key is given, there is a unique corresponding Value, and the Key and Value have some associations. Therefore, Map can be used as an associated array. The following is an Example of Test OS: Windows 7 Test IDE: Visual Studio 2005 Code Example: [cpp] # include "stdafx. h "# include <iostream> # include <map> # include <string> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {/* Create map/associative map *-keys are strings *-values are float */typedef map <string, float> StringFloatMap; // create empty container StringFloatMap stocks; // insert some elements Stocks ["TXKG"] = 276.50; stocks ["GOOG"] = 700.34; stocks ["AAPL"] = 498.26; stocks ["BMW"] = 823.23; stocks ["NONE"] = 0.72; stocks ["NOKIA"] = 12.25; // print all elements StringFloatMap: iterator pos; for (pos = stocks. begin (); pos! = Stocks. end (); ++ pos) {cout <"stock:" <pos-> first <"\ t" <"price: "<pos-> second <endl;} cout <endl; // all stocks doubled for (pos = stocks. begin (); pos! = Stocks. end (); ++ pos) {pos-> second * = 2;} // print all elements for (pos = stocks. begin (); pos! = Stocks. end (); ++ pos) {cout <"stock:" <pos-> first <"\ t" <"price: "<pos-> second <endl;} cout <endl; // rename key from "AAPL" to "APPLE" stocks ["APPLE"] = stocks ["AAPL"]; stocks. erase ("AAPL"); // print all elements for (pos = stocks. begin (); pos! = Stocks. end (); ++ pos) {cout <"stock:" <pos-> first <"\ t" <"price: "<pos-> second <endl ;}cout <endl; system (" pause "); return 0 ;}# include" stdafx. h "# include <iostream> # include <map> # include <string> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {/* Create map/associative map *-keys are strings *-values are float */typedef map <string, float> StringFloatMap; // create emp Ty containerStringFloatMap stocks; // insert some elementsstocks ["TXKG"] = 276.50; stocks ["GOOG"] = 700.34; stocks ["AAPL"] = 498.26; stocks ["BMW"] = 823.23; stocks ["NONE"] = 0.72; stocks ["NOKIA"] = 12.25; // print all elementsStringFloatMap: iterator pos; for (pos = stocks. begin (); pos! = Stocks. end (); ++ pos) {cout <"stock:" <pos-> first <"\ t" <"price: "<pos-> second <endl;} cout <endl; // all stocks doubledfor (pos = stocks. begin (); pos! = Stocks. end (); ++ pos) {pos-> second * = 2;} // print all elementsfor (pos = stocks. begin (); pos! = Stocks. end (); ++ pos) {cout <"stock:" <pos-> first <"\ t" <"price: "<pos-> second <endl;} cout <endl; // rename key from "AAPL" to "APPLE" stocks ["APPLE"] = stocks ["AAPL"]; stocks. erase ("AAPL"); // print all elementsfor (pos = stocks. begin (); pos! = Stocks. end (); ++ pos) {cout <"stock:" <pos-> first <"\ t" <"price: "<pos-> second <endl;} cout <endl; system (" pause "); return 0;} Test Result as follow: visible, the first element type of pair is used as the subscript to address the reference of the Value corresponding to the Key, so that you can easily manipulate the element. Note: when the Key is used as the lower mark, if the element already exists in the Map, the reference of the element Value is returned. If the element does not exist, the following Key element is inserted, use the Default constructor corresponding to the Value type to initialize the Value. In the preceding example, if the stocks ["abcd"] = 5.0 clause is used, the Key is inserted as the "abcd" element and initialized with float 0, assign the Value of this element to 5.0.