C++ Map运用实例

余生颓废 提交于 2020-08-16 22:20:56

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

请按任意键继续. . .

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!