STL-Container-Map (ii)

Source: Internet
Author: User

Using map as an associative array

MapAdvanceTest.cpp

#include <map>#include<string>#include<iostream>#include<iomanip>#include"MapAdvanceTest.h"#include".. /.. /core/containerutil.h"using namespacestd;voidMapadvancetest::useasassociativearray () {//Create map/associative Array//-Keys are strings//-values are floatstypedef map<string,float>Stringfloatmap;      Stringfloatmap stocks; //Create empty container//Insert some elementsstocks["BASF"] =369.50; stocks["VW"] =413.50; stocks["Daimler"] =819.00; stocks["BMW"] =834.00; stocks["Siemens"] =842.20; //Print all elementsContainerutil<stringfloatmap>::p rintmapindiv (Stocks,"Stock"," Price", the); //boom (all prices doubled)Stringfloatmap::iterator POS;  for(pos = Stocks.begin (); pos! = Stocks.end (); + +POS) {POS->second *=2; }    //Print all elementsContainerutil<stringfloatmap>::p rintmapindiv (Stocks,"Stock"," Price", the); //rename key from "Volkswagen" to "Volkswagen"//-provided only by exchanging elementstocks["Volkswagen"] = stocks["VW"]; Stocks.erase ("VW"); //Print all elementsContainerutil<stringfloatmap>::p rintmapindiv (Stocks,"Stock"," Price", the);}voidMapadvancetest::run () {Printstart ("Useasassociativearray ()");    Useasassociativearray (); Printend ("Useasassociativearray ()");}

Operation Result:

----------------Useasassociativearray (): Run Start----------------
STOCK:BASF price:369.5
STOCK:BMW price:834
Stock:daimler price:819
Stock:siemens price:842.2
STOCK:VW price:413.5

STOCK:BASF price:739
STOCK:BMW price:1668
Stock:daimler price:1638
Stock:siemens price:1684.4
STOCK:VW price:827

STOCK:BASF price:739
STOCK:BMW price:1668
Stock:daimler price:1638
Stock:siemens price:1684.4
Stock:volkswagen price:827

----------------Useasassociativearray (): Run End----------------

STL-Container-Map (ii)

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.