C++ Map运用实例
#include
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)<
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
请按任意键继续. . .