2017-08-19 10:58:52
writer;pprp
#include <map> #include <string> #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef pair<int, string> PAIR; ostream & operator<<(ostream & out, const PAIR& p) { out << p.first << " " << p.second << endl; return out; } //函数对象,对()进行了重载 struct CmpByValue { bool operator()(const PAIR&l, const PAIR&r) { return l.second < r.second; } }; int main() { //数据插入 map<int, string> mapStudent; mapStudent.insert(map<int, string>::value_type (1, "student_one")); mapStudent.insert(map<int, string>::value_type (2, "student_two")); mapStudent.insert(map<int, string>::value_type (3, "student_three")); mapStudent.insert(map<int, string>::value_type (4,"student_four")); mapStudent.insert(pair<int, string>(6, "student_six")); mapStudent.insert(make_pair(7, "student_seven")); mapStudent[5] = "student_five"; //数据遍历 //正向遍历 map<int, string>::iterator iter; for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) { cout<<iter->first<<" "<<iter->second<<endl; } //反向遍历 map<int, string>::reverse_iterator it; for(it = mapStudent.rbegin(); it != mapStudent.rend() ; it++) { cout << it->first <<" " << it->second << endl; } //数组形式的遍历 int nSize = mapStudent.size(); for(int i = 0 ; i < nSize ; i++) { cout << mapStudent[i] << endl; } cout << endl; //数据的查找 map<int, string>::iterator iter1; iter1 = mapStudent.find(1); if(iter1 != mapStudent.end()) { cout << iter1->second << endl; } else { cout << "can't find it " << endl; } //判断是否存在这个key int judge = mapStudent.count(1); if(judge) { cout << "exist" << endl; } else { cout << "not exist" << endl; } cout << endl; //用lower_bound进行查找 map<int, string> :: iterator it2; it2 = mapStudent.lower_bound(2); cout << it2->second << endl; it2 = mapStudent.upper_bound(2); cout << it2->second << endl; pair<map<int, string>::iterator, map<int, string>::iterator> mapair; mapair = mapStudent.equal_range(1); //如果相等的话那就说明没有找到,如果不等就说明找到了 if(mapair.first == mapair.second) cout << "can't find it" << endl; else cout << "the value of it is " << mapair.first->second << endl; //数据的清空与判空 // mapStudent.clear(); if(!mapStudent.empty()) cout << "not empty" << endl; else cout << "empty" << endl; //数据的删除 map<int, string> :: iterator it3; it3 = mapStudent.find(1); mapStudent.erase(it3); for(it3 = mapStudent.begin(); it3 != mapStudent.end(); it3++) cout << it3->second << endl; judge = mapStudent.erase(2); if(judge) { cout << "delete successfully" << endl; for(it3 = mapStudent.begin(); it3 != mapStudent.end(); it3++) { cout << it3->second << endl; } } //排序(按照key的大小排序) //map<int, string, less<int> >是默认的升序 //map<int, string, greater<int> >是可以构造的降序 //排序(按照value的大小排序) vector <PAIR> sortByValue(mapStudent.begin(), mapStudent.end()); sort(sortByValue.begin(), sortByValue.end(),CmpByValue()); for(int i = 0 ; i < mapStudent.size(); i++) { cout << sortByValue[i]<< endl; } return 0; }
另外multimap用法与map类似,函数什么的都一样,只是支持一个key对多个value
/* name : usage of List writer : pprp declare : null date ; 2017/8/20 */ #include <bits/stdc++.h> using namespace std; void print(multimap<string,double>&k) { multimap<string, double>::iterator it; for(it = k.begin(); it != k.end() ; it++) { cout << it->first << " " << it->second << endl; } } int main() { multimap<string, double>mp; mp.insert(pair<string,double>("jack",300.23)); mp.insert(make_pair("green",234.1)); mp.insert(make_pair("red",234.132)); mp.insert(make_pair("yellow",2342.1)); mp.insert(make_pair("blue",234.11)); mp.insert(make_pair("orange",2324.1)); multimap<string, double>::iterator it; print(mp); mp.erase("jack"); print(mp); it = mp.find("orange"); if(it != mp.end()) { cout << it->first << " " << it->second << endl; } return 0; }