官术网_书友最值得收藏!

Code walkthrough

The following line declares a map with a string name as the key and a long mobile number as the value stored in the map:

map< string, long > contacts;

The following code snippet adds four contacts organized by name as the key:

 contacts[ "Jegan" ] = 1234567890;
contacts[ "Meena" ] = 5784433221;
contacts[ "Nitesh" ] = 4567891234;
contacts[ "Sriram" ] = 8901122334;

The following line will try to locate the contact with the name, Sriram, in the contacts map; if Sriram is found, then the find() function will return the iterator pointing to the location of the key-value pair; otherwise it returns the contacts.end() position:

 auto pos = contacts.find( "Sriram" );

The following code verifies whether the iterator, pos, has reached contacts.end() and prints the contact number. Since the map is an associative container, it stores a key=>value pair; hence, pos->first indicates the key and pos->second indicates the value:

 if ( pos != contacts.end() )
cout << "\nMobile number of " << pos->first << " is " << pos->second << endl;
else
cout << "\nContact not found." << endl;
主站蜘蛛池模板: 阿荣旗| 洛川县| 朝阳县| 三亚市| 页游| 绥芬河市| 滕州市| 微山县| 隆安县| 云南省| 霞浦县| 日土县| 岢岚县| 宝坻区| 库尔勒市| 金坛市| 东明县| 松滋市| 孟津县| 运城市| 绿春县| 龙门县| 夏津县| 普陀区| 新源县| 吴桥县| 从江县| 固始县| 沙湾县| 永善县| 和平县| 丹东市| 隆安县| 大兴区| 潜江市| 宜川县| 九江市| 大竹县| 抚顺县| 沈丘县| 凌海市|