- 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;
推薦閱讀
- Building Modern Web Applications Using Angular
- Mastering JBoss Enterprise Application Platform 7
- Getting Started with React Native
- Babylon.js Essentials
- C語言程序設計與應用(第2版)
- 運維前線:一線運維專家的運維方法、技巧與實踐
- 算法圖解
- 深入實踐DDD:以DSL驅動復雜軟件開發
- INSTANT Apache ServiceMix How-to
- Greenplum構建實時數據倉庫實踐
- Java從入門到精通(視頻實戰版)
- 零基礎C語言學習筆記
- Neo4j Graph Data Modeling
- 威脅建模:設計和交付更安全的軟件
- Java程序員面試筆試真題庫