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