Map learning in C ++

Source: Internet
Author: User
Description:

Enter information about N students and then query them.

Input:

The first Act N entered, that is, the number of students (n <= 1000)

The next n rows contain information about N students. The information format is as follows: 01 Li Jiangnan 2102 Liu Tang male 2303 Zhang Jun male 1904 Wang na female 19 then input a m (M <= 10000 ), next, there will be m rows, representing M queries. Each row will enter a student ID in the following format: 02030104
Output:

Output m rows. Each row contains the information of a student corresponding to the query.

If no student information exists, "no answer!" Is output !"
Sample input:
401 Li Jiang male 2102 Liu Tang male 2303 Zhang Jun male 1904 Wang na female 1950203010403
Sample output:
02 Liu Tang male 2303 Zhang Jun male 1901 Li Jiang male 2104 Wang na female 1903 Zhang Jun male 19


A simple question.
It is used to practice C ++ map and string usage.
In any case, it's still easy to write without Java.

Learning statement:
1. Map <string, string >:: iterator ITER; // little understanding of C ++.
2、 m.insert(pair<string,string>(id,info));  
3. What is the name of the key and value respectively starting with ITER-> first ITER-> second //? To tell the truth, it's really frustrating. No wonder someone says C ++ is rubbish.

4. iter = M. Find (ID );

   if(iter != m.end()) 

 

#include <iostream>
#include <stdio.h>
#include <map>
#include <string>
using namespace std;

int main() {
string id,info,str;
int index,n,M;
map<string,string> m;
map<string,string>:: iterator iter;
while(cin >> n){
cin.ignore();
m.clear();
for(int i=0; i<n; i++){
getline(cin,str);
index = str.find(" ");
id = str.substr(0,index);
info = str.substr(index+1);
m.insert(pair<string,string>(id,info));
}
cin >> M;
for(int i=0; i<M; i++){
cin >> id;
iter = m.find(id);
if(iter != m.end())
cout << iter->first <<" "<<iter->second<<endl;
else
cout<<"No Answer!"<<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.