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

  • Mastering C++ Programming
  • Jeganathan Swaminathan
  • 171字
  • 2021-07-02 18:28:52

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;
主站蜘蛛池模板: 洪湖市| 景谷| 儋州市| 淮南市| 津市市| 中卫市| 南漳县| 盱眙县| 桓仁| 唐河县| 交口县| 西贡区| 潢川县| 绵竹市| 河间市| 新泰市| 洛浦县| 阿尔山市| 龙门县| 梁河县| 长泰县| 兴国县| 庆元县| 荆门市| 勐海县| 抚顺县| 麻城市| 赤水市| 若羌县| 通州区| 西乌珠穆沁旗| 北碚区| 武功县| 德庆县| 滨州市| 马山县| 兴义市| 苍梧县| 仁布县| 民丰县| 会宁县|