北京大學C語言學習第9天

STL 初步(二)
multimap的用法
multimap容器裏的元素,都是pair形式的
multimap<T1,T2> mp;
則mp裏的元素都是如下類型:
struct {
T1 first; //關鍵字
T2 second; //值
};
multimap中的元素按照first排序,並可以按first進行查找
缺省的排序規則是 “a.first < b.first” 爲true,則a排在b前面
5
一個學生成績錄入和查詢系統,接受以下兩種輸入:
Add name id score
Query score
name是個不超過16字符的字符串,中間沒有空格,代表學生姓名。id
是個整數,代表學號。score是個整數,表示分數。學號不會重複,分數
和姓名都可能重複。
兩種輸入交替出現。第一種輸入表示要添加一個學生的信息,碰到這
種輸入,就記下學生的姓名、id和分數。第二種輸入表示要查詢,碰到這
種輸入,就輸出已有記錄中分數比score低的最高分獲得者的姓名、學號
和分數。如果有多個學生都滿足條件,就輸出學號最大的那個學生的信
息。如果找不到滿足條件的學生,則輸出“Nobody”
multimap的應用
輸入樣例:
Add Jack 12 78
Query 78
Query 81
Add Percy 9 81
Add Marry 8 81
Query 82
Add Tom 11 79
Query 80
Query 81
輸出樣例:
Nobody
Jack 12 78
Percy 9 81
Tom 11 79
Tom 11 79

#include <iostream>
#include <map> //使用multimap和map需要包含此頭文件
#include <cstring>
using namespace std;
struct StudentInfo {
int id;
char name[20];
};
struct Student {
int score;
StudentInfo info; 
};
typedef multimap<int,StudentInfo> MAP_STD; 
// 此後 MAP_STD 等價於 multimap<int,StudentInfo>
// typedef int * PINT;
// 則此後 PINT 等價於 int *。 即 PINT p; 等價於 int * p;
int main() {
MAP_STD mp;
Student st;
char cmd[20];
while( cin >> cmd ) {
if( cmd[0] == 'A') {
cin >> st.info.name >> st.info.id >> st.score ;
mp.insert(make_pair(st.score,st.info ));
} //make_pair生成一個 pair<int,StudentInfo>變量
//其first 等於 st.score, second 等於 st.info
else if( cmd[0] == 'Q' ){
int score;
cin >> score;
MAP_STD::iterator p = mp.lower_bound (score);
if( p!= mp.begin()) { 
--p;
score = p->first; //比要查詢分數低的最高分
MAP_STD::iterator maxp = p; 
int maxId = p->second.id; 
for(; p != mp.begin() && 
p->first == score; --p) {
//遍歷所有成績和score相等的學生
if( p->second.id > maxId ) {
maxp = p;
maxId = p->second.id ;
}
}
if( p->first == score) { 
//如果上面循環是因爲 p == mp.begin() 而終止,則p指向的元素還要處理
if( p->second.id > maxId ) {
maxp = p;
maxId = p->second.id ;
}
}
cout << maxp->second.name << " " 
<< maxp->second.id << " " 
<< maxp->first << endl;
}
//lower_bound的結果就是 begin,說明沒人分數比查詢分數低
else cout << "Nobody" << endl;
}
}
return 0;
} 

map
map的用法
和multimap區別在於:
 不能有關鍵字重複的元素
 可以使用 [] ,下標爲關鍵字,返回值爲first和關鍵字相同的元
素的second
 插入元素可能失敗
13

#include <iostream>
#include <map> 
#include <string>
using namespace std;
struct Student {
string name;
int score;
};
Student students[5] = { 
{"Jack",89},{"Tom",74},{"Cindy",87},{"Alysa",87},{"Micheal",98}}; 
typedef map<string,int> MP;
int main()
{
MP mp; 
for(int i = 0;i < 5; ++i)
mp.insert(make_pair(students[i].name,students[i].score));
cout << mp["Jack"] << endl; // 輸出 89
mp["Jack"] = 60; //修改名爲"Jack"的元素的second 
for(MP::iterator i = mp.begin(); i != mp.end(); ++i)
cout << "(" << i->first << "," << i->second << ") ";
//輸出:(Alysa,87) (Cindy,87) (Jack,60) (Micheal,98) (Tom,74)
cout << endl;
Student st;
st.name = "Jack";
st.score = 99;
pair<MP::iterator, bool> p =
mp.insert(make_pair(st.name,st.score));
if( p.second ) 
cout << "(" << p.first->first << "," 
<< p.first->second << ") inserted" <<endl;
else
cout << "insertion failed" << endl; //輸出此信息
mp["Harry"] = 78; //插入一元素,其first爲"Harry",然後將其second改爲78 
MP::iterator q = mp.find("Harry");
cout << "(" << q->first << "," << q->second <<")" <<endl;
//輸出 (Harry,78)
return 0;
}

map例題:單詞詞頻統計程序
輸入大量單詞,每個單詞,一行,不超過20字符,沒有
空格。 按出現次數從多到少輸出這些單詞及其出現次數
。出現次數相同的,字典序靠前的在前面
輸入樣例:
this
is
ok
this
plus
that
is
plus
plus
輸出樣例:
plus 3
is 2
this 2
ok 1
that 1

#include <iostream>
#include <set>
#include <map>
#include <string>
using namespace std;
struct Word {
int times;
string wd;
};
struct Rule {
bool operator () ( const Word & w1,const Word & w2) const {
if( w1.times != w2.times)
return w1.times > w2.times;
else
return w1.wd < w2.wd;
}
};
int main()
{
string s;
set<Word,Rule> st;
map<string,int> mp;
while( cin >> s ) 
++ mp[s] ;
for( map<string,int>::iterator i = mp.begin(); 
i != mp.end(); ++i) {
Word tmp;
tmp.wd = i->first;
tmp.times = i->second;
st.insert(tmp);
}
for(set<Word,Rule>::iterator i = st.begin(); 
i != st.end(); ++i) 
cout << i->wd << " " << i->times << endl;
}
return 0;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章