#include<map>
#include<string>
#include<iostream>
using namespace std;
int main()
{
typedef map<string, double> Commodity_VS_Price;
Commodity_VS_Price commodity;
commodity["soap"] = 4.5;
commodity["towel"] = 12.0;
commodity.insert( pair<string, double>("toothpaste", 15.9) );
commodity.insert( pair<string, double>("toothbrush", 6.9) );
commodity.insert( map<string, double>::value_type("gel", 20.0) );
commodity.insert( map<string, double>::value_type("shampoo", 50.0) );
map<string,double>::iterator ipointer;
for(ipointer = commodity.begin(); ipointer!=commodity.end(); ipointer++)
{
//Sort in alphabetical order
cout<<"commodity: "<<ipointer->first<<" Price: "<<ipointer->second<<" ";
cout<<commodity[ipointer->first]<<endl;
}
return 0;
}
运行结果:
commodity: gel Price: 20 20
commodity: shampoo Price: 50 50
commodity: soap Price: 4.5 4.5
commodity: toothbrush Price: 6.9 6.9
commodity: toothpaste Price: 15.9 15.9
commodity: towel Price: 12 12