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

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;
主站蜘蛛池模板: 丁青县| 武冈市| 苍溪县| 东方市| 惠州市| 潍坊市| 福贡县| 凤阳县| 华阴市| 大洼县| 隆回县| 三亚市| 太保市| 淮南市| 杭锦后旗| 枣庄市| 遂溪县| 富平县| 霍城县| 疏勒县| 广宗县| 且末县| 湖南省| 吉木乃县| 台州市| 原阳县| 石首市| 潮州市| 巩留县| 苍溪县| 阿拉善盟| 安新县| 阿克| 揭阳市| 赤峰市| 广平县| 镇安县| 三原县| 通城县| 镶黄旗| 连山|