multimap的基本操作

#include <iostream>
#include <map>
#include <string>
using namespace std;

//struct info
//{
// string name;
// float score;
// bool operator < (const info &a) const{
//  return a.score < score;
// }
//};

//int main(int argc,int **argv){
////map元素的創建、插入、遍歷
//  map<string,float> m;
//  m["Jack"] = 98.5;
// m["Body"] = 96.0;
// m["Navy"] = 97.5;
// m["Jack"] = 90.5;
// m["Demo"] = 99.9;
// map<string,float>::iterator iter;
// for (iter = m.begin();iter!=m.end();++iter)
//  cout<<(*iter).first<<" : "<<(*iter).second<<endl;
////map元素的刪除
// m.erase("Body");
// cout<<"刪除元素後,map的內容爲:"<<endl;
// for (iter = m.begin();iter!=m.end();++iter)
//  cout<<(*iter).first<<" : "<<(*iter).second<<endl;
////map的逆向遍歷
// cout<<"逆向遍歷map:"<<endl;
// map<string,float>::reverse_iterator riter;
// for (riter = m.rbegin();riter!=m.rend();++riter)
//  cout<<(*riter).first<<" : "<<(*riter).second<<endl;
// cout<<endl;
////元素的搜索
// map<string,float>::iterator iter1;
// iter1 = m.find("Demo");
// if (iter1!=m.end())
//  cout<<(*iter1).first<<" : "<<(*iter1).second<<endl;
// else{
//  cout<<"can not find!"<<endl;
// }
// return 0;
//
//}

//int main(){
//
// map<info,int> m;
// info information;
// 
// information.name = "Jack";
// information.score = 98.5;
// m[information] = 25;
//
// information.name = "Body";
// information.score = 96.5;
// m[information] = 10;
//
// information.name = "Navy";
// information.score = 97.5;
// m[information] = 30;
//
// information.name = "Demo";
// information.score = 99.5;
// m[information] = 70;
//
// map<info,int>::iterator iter;
// for (iter = m.begin();iter!=m.end();++iter){
//  cout<<(*iter).second<<" : ";
//  cout<<((*iter).first).name<<"  "<<((*iter).first).score<<endl;
// }
// return 0;
//}

int main(int argc,int **argv){

//multimap的創建、插入、遍歷
 multimap <string ,double> m;
 
 m.insert(pair<string,double>("Jack",96.5));
 m.insert(pair<string,double>("Body",95.5));
 m.insert(pair<string,double>("Navy",94.5));
 m.insert(pair<string,double>("Demo",99.5));
 
 multimap<string,double>::iterator iter;
 for (iter = m.begin();iter != m.end();++iter)
  cout<<(*iter).first<<"  "<<(*iter).second<<endl;

//multimap的刪除
 m.erase("Jack");
 cout<<"The element after delete:"<<endl;
 for (iter = m.begin();iter != m.end();++iter)
  cout<<(*iter).first<<"  "<<(*iter).second<<endl;

//multimap元素的查找 
 multimap<string,double>::iterator it;
 it = m.find("Navy");
 cout<<"the search result is :"<<endl;
 if (it!=m.end())
 {
  cout<<(*it).first<<"  "<<(*it).second<<endl;
 }
 else{
  cout<<"can not find!"<<endl;
 }
 return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章