Map object creates a query for the family name

Source: Internet
Author: User

Question: define a map object. The key of the element is the family name, and the value is the vector object that stores the family name. Enter at least six entries for the map container. Check your program by querying based on the family name. The query should output the names of all the children in the family.

// Define a map object. The key of the element is the family name. // The value is the vector object that stores the family name. // You can query the family name based on the family name, output the names of all children in the family # include <iostream> # include <map> # include <vector> # include <string> using namespace std; int main () {map <string, vector <string> children; string surname, childName; // read the entry (family name and all children's names) do {cout <"Enter surname:" <endl; cin> surname; if (! Cin) // read and end break; // Insert a new entry vector <string> chd; pair <map <string, vector <string >:: iterator, bool> ret = children. insert (make_pair (surname, chd); if (! Ret. second) {// This family name already exists in the map container cout <"repeated surname:" <surname <endl; continue ;} cout <"Enter children's name:" <endl; while (cin> childName) // read the names of all children in the family, ret. first-> second. push_back (childName); cin. clear (); // make the input stream valid again} while (cin); cin. clear (); // make the input stream valid again // read the cout family to be queried <"Enter a surname to search:" <endl; cin> surname; // search for map <string, vector <string> >:: iterator iter = children. Find (surname); // output the query result if (iter = children. end () // unable to find the family name cout <"no this surname:" <surname <endl; else {cout <"children:" <endl; // output the names of all children in the family. vector <string >:: iterator it = iter-> second. begin (); while (it! = Iter-> second. end () cout <* it ++ <endl;} return 0 ;}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.