C+---map、multimapの使用

43196 ワード

  • 基本概念Mapの特性は、すべての要素が要素のキー値に基づいて自動的にソートされることである.Mapのすべての要素はpairであり、同時に実値とキー値を有し、pairの第1の要素はキー値と見なされ、第2の要素は実値と見なされ、mapは2つの要素が同じキー値を持つことを許さない.mapの反復器でmapのキー値を変更できますか?答えはだめです.mapのキー値はmap要素の配列規則に関係しているので、mapのキー値を任意に変更するとmap組織が深刻に破壊されます.要素の実値を変更する場合は、可能です.Mapとlistは同じいくつかの性質を持っており、コンテナ要素を新規操作または削除すると、操作前のすべての反復器は、操作が完了した後も有効であり、もちろん削除された要素の反復器は例外である.Multimapはmapと同様に動作し、multimapキー値を繰り返すことができます.Mapとmultimapはいずれも赤と黒の木を底層として実現するメカニズムである.
  • 共通API
  • 構造関数
  • map<T1, T2> mapTT;//map      : 
    map(const map &mp);//      
    
    
  • map割付操作
  • map& operator=(const map &mp);//       
    swap(mp);//        
    
  • サイズ操作
  • size();//          
    empty();//        
    
  • 挿入データ
  • map.insert(...); //       ,  pair
    map mapStu;
    //       pair       
    mapStu.insert(pair(3, "  "));
    //       pair       
    mapStu.inset(make_pair(-1, "  "));
    //       value_type       
    mapStu.insert(map::value_type(1, "  "));
    //               
    mapStu[3] = "  ";
    mapStu[5] = "  ";
    
    
  • map削除操作
  • clear();//      
    erase(pos);//  pos        ,           。
    erase(beg,end);//    [beg,end)      ,           。
    erase(keyElem);//     key keyElem   。
    
    
  • ルックアップ動作
  • find(key);//   key    ,   ,           ;/    ,  map.end();
    count(keyElem);//     key keyElem     。 map  ,   0,   1。 multimap  ,     1。
    lower_bound(keyElem);//     key>=keyElem      。
    upper_bound(keyElem);//     key>keyElem      。
    equal_range(keyElem);//     key keyElem            。
    
    

    ケース
    #define _CRT_SECURE_NO_WARNINGS
    
    #include
    #include
    #include
    #include
    using namespace std;
    
    //multimap   
    //        55//     :               
    //   Multimap              
    //                  
    
    
    #define SALE_DEPATMENT 1 //    
    #define DEVELOP_DEPATMENT 2 //    
    #define FINACIAL_DEPATMENT 3 //    
    #define ALL_DEPATMENT 4 //    
    
    //   
    class person{
    public:
    	string name; //    
    	int age; //    
    	double salary; //    
    	string tele; //    
    };
    
    //  5   
    void CreatePerson(vector<person>& vlist){
    
    	string seed = "ABCDE";
    	for (int i = 0; i < 5; i++){
    		person p;
    		p.name = "  ";
    		p.name += seed[i];
    		p.age = rand() % 30 + 20;
    		p.salary = rand() % 20000 + 10000;
    		p.tele = "010-8888888";
    		vlist.push_back(p);
    	}
    
    }
    
    //5           
    void PersonByGroup(vector<person>& vlist, multimap<int, person>& plist){
    
    
    	int operate = -1; //     
    
    	for (vector<person>::iterator it = vlist.begin(); it != vlist.end(); it++){
    
    		cout << "      :" << endl;
    		cout << "  :" << it->name << "   :" << it->age << "   :" << it->salary << "   :" << it->tele << endl;
    		cout << "           (1     , 2     , 3     ):" << endl;
    		scanf("%d", &operate);
    
    		while (true){
    #define _CRT_SECURE_NO_WARNINGS
    
    #include
    #include
    #include
    #include
    using namespace std;
    
    //multimap   
    //        55//     :               
    //   Multimap              
    //                  
    
    
    #define SALE_DEPATMENT 1 //    
    #define DEVELOP_DEPATMENT 2 //    
    #define FINACIAL_DEPATMENT 3 //    
    #define ALL_DEPATMENT 4 //    
    
    //   
    class person{
    public:
    	string name; //    
    	int age; //    
    	double salary; //    
    	string tele; //    
    };
    
    //  5   
    void CreatePerson(vector<person>& vlist){
    
    	string seed = "ABCDE";
    	for (int i = 0; i < 5; i++){
    		person p;
    		p.name = "  ";
    		p.name += seed[i];
    		p.age = rand() % 30 + 20;
    		p.salary = rand() % 20000 + 10000;
    		p.tele = "010-8888888";
    		vlist.push_back(p);
    	}
    
    }
    
    //5           
    void PersonByGroup(vector<person>& vlist, multimap<int, person>& plist){
    
    
    	int operate = -1; //     
    
    	for (vector<person>::iterator it = vlist.begin(); it != vlist.end(); it++){
    
    		cout << "      :" << endl;
    		cout << "  :" << it->name << "   :" << it->age << "   :" << it->salary << "   :" << it->tele << endl;
    		cout << "           (1     , 2     , 3     ):" << endl;
    		scanf("%d", &operate);
    
    		while (true){
    
    			if (operate == SALE_DEPATMENT){  //           
    				plist.insert(make_pair(SALE_DEPATMENT, *it));
    				break;
    			}
    			else if (operate == DEVELOP_DEPATMENT){
    				plist.insert(make_pair(DEVELOP_DEPATMENT, *it));
    				break;
    			}
    			else if (operate == FINACIAL_DEPATMENT){
    				plist.insert(make_pair(FINACIAL_DEPATMENT, *it));
    				break;
    			}
    			else{
    				cout << "      ,     (1     , 2     , 3     ):" << endl;
    				scanf("%d", &operate);
    			}
    
    		}
    
    	}
    	cout << "        !" << endl;
    	cout << "***********************************************************" << endl;
    
    }
    
    //      
    void printList(multimap<int, person>& plist, int myoperate){
    
    	if (myoperate == ALL_DEPATMENT){
    		for (multimap<int, person>::iterator it = plist.begin(); it != plist.end(); it++){
    			cout << "  :" << it->second.name << "   :" << it->second.age << "   :" << it->second.salary << "   :" << it->second.tele << endl;
    		}
    		return;
    	}
    
    	multimap<int, person>::iterator it = plist.find(myoperate);
    	int depatCount = plist.count(myoperate);
    	int num = 0;
    	if (it != plist.end()){
    		while (it != plist.end() && num < depatCount){
    			cout << "  :" << it->second.name << "   :" << it->second.age << "   :" << it->second.salary << "   :" << it->second.tele << endl;
    			it++;
    			num++;
    		}
    	}
    }
    
    //                 
    void ShowPersonList(multimap<int, person>& plist, int myoperate){
    
    	switch (myoperate)
    	{
    	case SALE_DEPATMENT:
    		printList(plist, SALE_DEPATMENT);
    		break;
    	case DEVELOP_DEPATMENT:
    		printList(plist, DEVELOP_DEPATMENT);
    		break;
    	case FINACIAL_DEPATMENT:
    		printList(plist, FINACIAL_DEPATMENT);
    		break;
    	case ALL_DEPATMENT:
    		printList(plist, ALL_DEPATMENT);
    		break;
    	}
    }
    
    //      
    void PersonMenue(multimap<int, person>& plist){
    
    	int flag = -1;
    	int isexit = 0;
    	while (true){
    		cout << "       ((1     , 2     , 3     , 4     , 0  ):" << endl;
    		scanf("%d", &flag);
    
    		switch (flag)
    		{
    		case SALE_DEPATMENT:
    			ShowPersonList(plist, SALE_DEPATMENT);
    			break;
    		case DEVELOP_DEPATMENT:
    			ShowPersonList(plist, DEVELOP_DEPATMENT);
    			break;
    		case FINACIAL_DEPATMENT:
    			ShowPersonList(plist, FINACIAL_DEPATMENT);
    			break;
    		case ALL_DEPATMENT:
    			ShowPersonList(plist, ALL_DEPATMENT);
    			break;
    		case 0:
    			isexit = 1;
    			break;
    		default:
    			cout << "      ,     !" << endl;
    			break;
    		}
    
    		if (isexit == 1){
    			break;
    		}
    	}
    
    }
    
    int main(){
    
    	vector<person>  vlist; //   5       
    	multimap<int, person> plist; //         
    
    	//  5   
    	CreatePerson(vlist);
    	//5           
    	PersonByGroup(vlist, plist);
    	//                                 
    	PersonMenue(plist);
    
    	system("pause");
    	return EXIT_SUCCESS;
    }