map member functions
搜了才发现map的成员函数这么多orz,跟着cplusplus按字典序走一遍叭(顺序有微调orz
<1> map::at (c++11)
// map::at //Returns a reference to the mapped value of the element identified with key k. //If k does not match the key of any element in the container, the function throws an out_of_range exception. #include <iostream> #include <string> #include <map> int main () { std::map<std::string,int> mymap = { { "alpha", 0 }, { "beta", 0 }, { "gamma", 0 } }; mymap.at("alpha") = 10; mymap.at("beta") = 20; mymap.at("gamma") = 30; for (auto& x: mymap) { std::cout << x.first << ": " << x.second << '\n'; } return 0; } /*output: alpha: 10 beta: 20 gamma: 30*/
<2> map::begin/end
// map::begin/end //Returns an iterator referring to the first element in the map container. //end同理 #include <iostream> #include <map> int main () { std::map<char,int> mymap; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; // show content: for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; } /*output a => 200 b => 100 c => 300*/
<3>map::cbegin(c++11)
// map::cbegin/cend #include <iostream> #include <map> int main () { std::map<char,int> mymap; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; // print content: std::cout << "mymap contains:"; for (auto it = mymap.cbegin(); it != mymap.cend(); ++it) std::cout << " [" << (*it).first << ':' << (*it).second << ']'; std::cout << '\n'; return 0; } /*output mymap contains: [a:200] [b:100] [c:300] */
<4> map::clear
Removes all elements from the map container (which are destroyed), leaving the container with a size of 0.
//很简单所以就不转例子了orz
<5>map::count
Searches the container for elements with a key equivalent to k and returns the number of matches.
Because all elements in a map container are unique, the function can only return 1 (if the element is found) or zero (otherwise).
Two keys are considered equivalent if the container's comparison object returns false reflexively (i.e., no matter the order in which the keys are passed as arguments).
// 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; }
<5>map::crbegin
Returns a const_reverse_iterator pointing to the last element in the container (i.e., its reverse beginning).
<6>map::crend
Returns a const_reverse_iterator pointing to the theoretical element preceding the first element in the container (which is considered its reverse end).
// map::crbegin/crend #include <iostream> #include <map> int main () { std::map<char,int> mymap; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; std::cout << "mymap backwards:"; for (auto rit = mymap.crbegin(); rit != mymap.crend(); ++rit) std::cout << " [" << rit->first << ':' << rit->second << ']'; std::cout << '\n'; return 0; } //output:mymap backwards: [c:300] [b:100] [a:200]
<7>map::emplace(c++11)
// map::emplace #include <iostream> #include <map> int main () { std::map<char,int> mymap; mymap.emplace('x',100); mymap.emplace('y',200); mymap.emplace('z',100); std::cout << "mymap contains:"; for (auto& x: mymap) std::cout << " [" << x.first << ':' << x.second << ']'; std::cout << '\n'; return 0; } //output :mymap contains: [x:100] [y:200] [z:100]
<8>map::emplace_hint
带有提示的位置插入?
// map::emplace_hint #include <iostream> #include <map> int main () { std::map<char,int> mymap; auto it = mymap.end(); it = mymap.emplace_hint(it,'b',10); mymap.emplace_hint(it,'a',12); mymap.emplace_hint(mymap.end(),'c',14); std::cout << "mymap contains:"; for (auto& x: mymap) std::cout << " [" << x.first << ':' << x.second << ']'; std::cout << '\n'; return 0; } //output:mymap contains: [a:12] [b:10] [c:14]
<9>map::empty
Returns whether the map container is empty (i.e. whether its size is 0).
<10>map::equal_range
<11>map::erase
可以通过iterator,key,range实现删除
// erasing from map #include <iostream> #include <map> int main () { std::map<char,int> mymap; std::map<char,int>::iterator it; // insert some values: mymap['a']=10; mymap['b']=20; mymap['c']=30; mymap['d']=40; mymap['e']=50; mymap['f']=60; 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 // show content: for (it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; } /*output: a => 10 d => 40 */
<12>map::find
//通过key查询,如果找到了返回对应值,没有找到返回末尾位置
// 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; } /*output: elements in mymap: a => 50 c => 150 d => 200 */
<13>map::get_allocator
//(这个函数好像用不多?先不写了
<14>map::insert
// map::insert (C++98) #include <iostream> #include <map> int main () { std::map<char,int> mymap; // first insert function version (single parameter): mymap.insert ( std::pair<char,int>('a',100) ); mymap.insert ( std::pair<char,int>('z',200) ); std::pair<std::map<char,int>::iterator,bool> ret; ret = mymap.insert ( std::pair<char,int>('z',500) ); if (ret.second==false) { std::cout << "element 'z' already existed"; std::cout << " with a value of " << ret.first->second << '\n'; } // 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')); // showing contents: std::cout << "mymap contains:\n"; for (it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; std::cout << "anothermap contains:\n"; for (it=anothermap.begin(); it!=anothermap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; } /*output: element 'z' already existed with a value of 200 mymap contains: a => 100 b => 300 c => 400 z => 200 anothermap contains: a => 100 b => 300 */
<15>map::lower_bound/upper_bound
// 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; } /*output: a => 20 e => 100 */
累辽,下次有空又不想写题再补坑吧,感觉还是写题把方法用上去比较快乐
不过平常用的稍微多一点的也差不多了orz,佛系更新