C++ Map运用实例
作者:互联网
C++ Map运用实例
#include <map> #include <string> #include <iostream> #include <iomanip> using namespace std; int main() { typedef map<string, float> stringFloatMap; //create empty container stringFloatMap stocks1; //插入元素 stocks1["BASF"] = 369.50; stocks1["VW"] = 413.50; stocks1["Daimler"] = 819.00; stocks1["BMW"] = 834.00; stocks1["Simements"] = 842.20; //输出 stringFloatMap::iterator iter1; cout << left; for (iter1 = stocks1.begin();iter1 != stocks1.end();++iter1) { cout << "stock1:"<<setw(12)<<iter1->first<<" price:"<<iter1->second << endl; } cout << endl; //所有 float统一变换 for (iter1 = stocks1.begin();iter1 != stocks1.end();++iter1) { iter1->second *= 2; } //输出 for (iter1 = stocks1.begin(); iter1 != stocks1.end(); ++iter1) { cout << "stock1:" << setw(12) << iter1->first << " price:" << iter1->second << endl; } cout << endl; stocks1["Volkswagen"] = stocks1["VW"]; stocks1.erase("VW"); //输出 for (iter1 = stocks1.begin(); iter1 != stocks1.end(); ++iter1) { cout << "stock1:" << setw(12) << iter1->first << " price:" << iter1->second << endl; } cout << endl; system("pause"); return 0; }
stock1:BASF price:369.5
stock1:BMW price:834
stock1:Daimler price:819
stock1:Simements price:842.2
stock1:VW price:413.5
stock1:BASF price:739
stock1:BMW price:1668
stock1:Daimler price:1638
stock1:Simements price:1684.4
stock1:VW price:827
stock1:BASF price:739
stock1:BMW price:1668
stock1:Daimler price:1638
stock1:Simements price:1684.4
stock1:Volkswagen price:827
请按任意键继续. . .
代码参考:C++标准库(第2版)
标签:Map,stocks1,price,C++,实例,BMW,stock1,iter1,Daimler 来源: https://www.cnblogs.com/herd/p/12063647.html