map

xiaoxiao2022-06-11  26

map
1 生成构造
// constructing maps #include <iostream> #include <map> bool fncomp (char lhs, char rhs) {return lhs<rhs;} struct classcomp { bool operator() (const char& lhs, const char& rhs) const {return lhs<rhs;} }; int main () { std::map<char,int> first; first['a']=10; first['b']=30; first['c']=50; first['d']=70; std::map<char,int> second (first.begin(),first.end()); std::map<char,int> third (second); std::map<char,int,classcomp> fourth; // class as Compare bool(*fn_pt)(char,char) = fncomp; std::map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare std::map<char,int> first; std::map<char,int> second; first['x']=8; first['y']=16; first['z']=32; second=first; // second now contains 3 ints first=std::map<char,int>(); // and first is now empty return 0; }
2 操作
2.1 大小和容量
/* * capacity * size max_size empty */ #include <iostream> #include <map> int main () { std::map<char,int> mymap; mymap['a']=10; mymap['b']=30; mymap['c']=50; mymap['d']=70; mymap.size(); mymap.max_size(); mymap.empty(); for(auto it=mymap.cbegin();it!=mymap.cend();++it) std::cout<<it->first<<"=>"<<it->second<<" \n"; return 0; }
2.2 比较
2.3 find
2.4 截取部分
2.5 增,插,删,改
/* * modify element * insert erase clear swap */ #include <iostream> #include <map> int main () { std::map<char,int> mymap; mymap['a']=10; mymap['b']=30; mymap['c']=50; mymap['d']=70; // first insert function version (single parameter): mymap.insert ( std::pair<char,int>('a',100) ); mymap.insert ( std::pair<char,int>('z',200) ); // second insert function version (with hint position): std::map<char,int>::iterator it = mymap.begin(); mymap.insert (it, std::pair<char,int>('b',300)); // max efficiency inserting mymap.insert (it, std::pair<char,int>('c',400)); // no max efficiency inserting // third insert function version (range insertion): std::map<char,int> anothermap; anothermap.insert(mymap.begin(),mymap.find('c')); it=mymap.find('b'); mymap.erase (it); // erasing by iterator mymap.erase ('c'); // erasing by key it=mymap.find ('e'); mymap.erase ( it, mymap.end() ); // erasing by range mymap.clear(); //mymap.swap(mymap2) return 0; }
3.6 access element
/* * access element * operator[] at */ #include <iostream> #include <map> int main () { std::map<char,int> mymap; mymap['a']=10; mymap['b']=30; mymap['c']=50; mymap['d']=70; std::cout<<mymap.at('a'); std::cout<<mymap['b']; return 0; }
2.7其他
//范围 // map::equal_range #include <iostream> #include <map> int main () { std::map<char,int> mymap; mymap['a']=10; mymap['b']=20; mymap['c']=30; std::pair<std::map<char,int>::iterator,std::map<char,int>::iterator> ret; ret = mymap.equal_range('b'); std::cout << "lower bound points to: "; std::cout << ret.first->first << " => " << ret.first->second << '\n'; std::cout << "upper bound points to: "; std::cout << ret.second->first << " => " << ret.second->second << '\n'; /* * output: * 下限指向:'b'=> 20 *上限指向:'c'=> 3 */ return 0; } // map::lower_bound/upper_bound #include <iostream> #include <map> int main () { std::map<char,int> mymap; std::map<char,int>::iterator itlow,itup; mymap['a']=20; mymap['b']=40; mymap['c']=60; mymap['d']=80; mymap['e']=100; itlow=mymap.lower_bound ('b'); // itlow points to b itup=mymap.upper_bound ('d'); // itup points to e (not d!) mymap.erase(itlow,itup); // erases [itlow,itup) // print content: for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; } //数量 // map::count #include <iostream> #include <map> int main () { std::map<char,int> mymap; char c; mymap ['a']=101; mymap ['c']=202; mymap ['f']=303; for (c='a'; c<'h'; c++) { std::cout << c; if (mymap.count(c)>0) std::cout << " is an element of mymap.\n"; else std::cout << " is not an element of mymap.\n"; } return 0; } // map::find #include <iostream> #include <map> int main () { std::map<char,int> mymap; std::map<char,int>::iterator it; mymap['a']=50; mymap['b']=100; mymap['c']=150; mymap['d']=200; it = mymap.find('b'); if (it != mymap.end()) mymap.erase (it); // print content: std::cout << "elements in mymap:" << '\n'; std::cout << "a => " << mymap.find('a')->second << '\n'; std::cout << "c => " << mymap.find('c')->second << '\n'; std::cout << "d => " << mymap.find('d')->second << '\n'; return 0; }
转载请注明原文地址: https://www.6miu.com/read-4931414.html

最新回复(0)