Associated array used by Map

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.