zoukankan      html  css  js  c++  java
  • STL(标准模板库)笔记——平衡二叉树map

    STL(标准模板库)笔记---平衡二叉树map

    本系列是观看北大郭炜老师程序与算法课程的笔记,用于复习与巩固。

    map

    map的用法

    和multimap区别在于:

    • 不能有关键字重复的元素
    • 可以使用 [] ,下标为关键字,返回值为first和关键字相同的元 素的second
    • 插入元素可能失败
    #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; 
    }
    
  • 相关阅读:
    hdu 1863 畅通工程
    pku 1275 Cashier Employment
    hdu 1879 继续畅通工程
    hdu 1301 Jungle Roads
    zoj1655 Transport Goods
    人生最大的遗憾,莫过于错误的坚持,和轻易的放弃……
    职业发展
    win8学习手册
    铭记这些题
    android 定制PreferenceScreen
  • 原文地址:https://www.cnblogs.com/shuai841952238/p/13154693.html
Copyright © 2011-2022 走看看