📄 map_operators.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta name="generator" content= "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"> <title>Map operators</title> <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table> <tr> <td> <div class="body-content"> <div class="header-box"> <a href="../index.html">cppreference.com</a> > <a href= "index.html">C++ Maps</a> > <a href="map_operators.html">Map operators</a> </div> <div class="name-format"> Map operators </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <map> <a href="../containers.html">TYPE</a>& operator[]( const <a href="../containers.html">key_type</a>& key ); map operator=(const map& c2); bool operator==(const map& c1, const map& c2); bool operator!=(const map& c1, const map& c2); bool operator<(const map& c1, const map& c2); bool operator>(const map& c1, const map& c2); bool operator<=(const map& c1, const map& c2); bool operator>=(const map& c1, const map& c2);</pre> <p>Maps can be compared and assigned with the standard comparison operators: ==, !=, <=, >=, <, >, and =. Individual elements of a map can be examined with the [] operator.</p> <p>Performing a comparison or assigning one map to another takes <a href="../complexity.html">linear time</a>.</p> <p>Two maps are equal if:</p> <ol> <li>Their size is the same, and</li> <li>Each member in location <em>i</em> in one map is equal to the the member in location <em>i</em> in the other map.</li> </ol> <p>Comparisons among maps are done lexicographically.</p> <p>For example, the following code defines a map between strings and integers and loads values into the map using the [] operator:</p> <pre class="example-code"> struct strCmp { bool operator()( const char* s1, const char* s2 ) const { return strcmp( s1, s2 ) < 0; } }; map<const char*, int, strCmp> ages; ages["Homer"] = 38; ages["Marge"] = 37; ages["Lisa"] = 8; ages["Maggie"] = 1; ages["Bart"] = 11; cout << "Bart is " << ages["Bart"] << " years old" << endl; cout << "In alphabetical order: " << endl; for( map<const char*, int, strCmp>::iterator iter = ages.begin(); iter != ages.end(); iter++ ) { cout << (*iter).first << " is " << (*iter).second << " years old" << endl; }</pre> <p>When run, the above code displays this output:</p> <pre class="example-code"> Bart is 11 years old In alphabetical order: Bart is 11 years old Homer is 38 years old Lisa is 8 years old Maggie is 1 years old Marge is 37 years old </pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="insert.html">insert</a><br> <a href="map_constructors.html">Map Constructors & Destructors</a> </div> </div> </td> </tr> </table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -