C++中的map學習

來源:互聯網
上載者:User
題目描述:

 輸入N個學生的資訊,然後進行查詢。

輸入:

 輸入的第一行為N,即學生的個數(N<=1000)

接下來的N行包括N個學生的資訊,資訊格式如下:01 李江 男 2102 劉唐 男 2303 張軍 男 1904 王娜 女 19然後輸入一個M(M<=10000),接下來會有M行,代表M次查詢,每行輸入一個學號,格式如下:02030104
輸出:

 輸出M行,每行包括一個對應於查詢的學生的資訊。

如果沒有對應的學生資訊,則輸出“No Answer!”
範例輸入:
401 李江 男 2102 劉唐 男 2303 張軍 男 1904 王娜 女 1950203010403
範例輸出:
02 劉唐 男 2303 張軍 男 1901 李江 男 2104 王娜 女 1903 張軍 男 19


很簡單的一題。
用來練習C++ map 以及 string 的用法。
不管怎麼說,還是沒有java寫起來順手

學習語句:
1、map<string,string>:: iterator iter; //對C++ 瞭解甚少,這句還不懂
2、 m.insert(pair<string,string>(id,info));  
3、的到key和value分別用iter->first   iter->second      // 起的什麼名啊,說實話真挫,難怪有人說C++垃圾了

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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.