标签:elements nta iso contain arguments key beta div contains
搜了才发现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
标签:elements nta iso contain arguments key beta div contains
原文地址:https://www.cnblogs.com/h404nofound/p/11620125.html