初始化:
map<string,double> salaries;
算法:
1. 赋值。salaries[ "Pat" ] = 75000.00;
2. 无效的索引将自动添加一项
cout << salaries[ "Jan" ] << endl;
3. Map的第一个或者第二个元素为数组的情况
代码:
1 #include <map> 2 #include <string> 3 #include <iostream> 4 using namespace std; 5 6 int main( ) 7 { 8 map<string,double> salaries; 9 10 salaries[ "Pat" ] = 75000.00; 11 cout << salaries[ "Pat" ] << endl; 12 cout << salaries[ "Jan" ] << endl; 13 14 map<string,double>::const_iterator itr; 15 itr = salaries.find( "Jan" ); 16 if( itr == salaries.end( ) ) 17 cout << "Not an employee of this company!" << endl; 18 else 19 cout << itr->second << endl; 20 21 return 0; 22 }
输出:
$ ./a.exe 75000 0 0