📄 cppmap_details.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <meta name="generator" content="HTML Tidy for Linux/x86 (vers 1st October 2002), see www.w3.org"> <title>C++ Maps</title> </head> <body bgcolor="#ffffff"> <table width="100%" bgcolor="#eeeeff"> <tr> <td><a href="index.html">cppreference.com</a> -> <a href="cppmap.html">C++ Maps</a> -> Details</td> </tr> </table> <h1>C++ Maps</h1> <p>C++ Maps can be used to store key/value pairs.</p> <hr> <h2><a name="pair">The pair struct</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> pair<KeyType,ValueType>; pair<KeyType,ValueType>( const KeyType &k, const ValueType &v ); pair<KeyType,ValueType> make_pair( KeyType &k, ValueType &v ); aPair->first; aPair->second; </pre> </td> </tr> </table> <p> The data type that a map actually contains is a pair. Each pair is templated to hold two pieces of data, which can be accessed via the <i>first</i> and <i>second</i> fields of the pair. Pairs can either be created directly (using a constructor) or with the <i>make_pair()</i> function. The advantage of using the make_pair function is that the types of the objects being stored do not have to be specified by programmer. </p> <hr> <h2><a name="Operators">Operators</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> == != < > <= >= []</pre> </td> </tr> </table> <p>Valid operators for C++ maps include the comparison operators <strong>==,!=,<,>,<=,</strong> and <strong>>=</strong> as well as the accessor operator <strong>[]</strong>. </p> <hr> <h2><a name="begin">begin</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">iterator</a> begin();</pre> </td> </tr> </table> <p>The function begin() returns an <a href="iterators.html">iterator</a> to the first element in the map. For example, the following code accepts some number of words from the user and displays how often each word occurs:</p><pre> map<string, int> frequencyCount; string s; // collect input from the user until they enter "quit" cin >> s; while( s != "quit" ) { frequencyCount[s]++; cin >> s; } // display the frequency of each word map<string, int>::const_iterator iter; for( iter=frequencyCount.begin(); iter != frequencyCount.end(); ++iter) { cout << iter->second << " " << iter->first << endl; }</pre> <hr> <h2><a name="clear">clear</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void clear();</pre> </td> </tr> </table> <p>The clear() function deletes all elements from the map.</p> <hr> <h2><a name="count">count</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> size_type count( const <a href="containers.html">KEY_TYPE</a> &key );</pre> </td> </tr> </table> <p>The function count() returns the number of occurrences of <i>key</i> in the map.</p> <hr> <h2><a name="empty">empty</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> bool empty();</pre> </td> </tr> </table> <p>The function empty() returns <strong>true</strong> if the map is empty, and <strong>false</strong> otherwise.</p> <hr> <h2><a name="end">end</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">iterator</a> end();</pre> </td> </tr> </table> <p>The end() function returns an <a href="iterators.html">iterator</a> to the end of the map.</p> <hr> <h2><a name="equal_range">equal_range</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> pair<iterator, iterator> equal_range( const <a href="containers.html">KEY_TYPE</a> &key );</pre> </td> </tr> </table> <p>The function equal_range() looks at a sorted version of the map and returns two iterators - one to the first element that contains <i>key</i>, another just past the last element that contains <i>key</i>. If <i>key</i> is not in the map, the two iterators will be equal.</p> <hr> <h2><a name="erase">erase</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void erase( <a href="iterators.html">iterator</a> pos ); void erase( <a href="iterators.html">iterator</a> start, <a href="iterators.html">iterator</a> end ); size_type erase( const <a href="containers.html">KEY_TYPE</a> &key );</pre> </td> </tr> </table> <p>The erase function() either erases the element at <i>pos</i>, erases the elements between <i>start</i> and <i>end</i>, or erases all elements that have the value of <i>key</i>.</p> <hr> <h2><a name="find">find</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">iterator</a> find( const <a href="containers.html">KEY_TYPE</a> &key );</pre> </td> </tr> </table> <p>The find() function returns an <a href="iterators.html">iterator</a> to <i>key</i>, or an <a href= "iterators.html">iterator</a> to the end of the map if <i>key</i> is not found. For example, the following code searches a map for a specific character and displays its associated ASCII value:</p><pre> map<char,int> characterMap; for( int i = 0; i < 26; i++ ) { characterMap.insert( pair<char,int>('A'+i, 65+i) ); characterMap.insert( pair<char,int>('a'+i, 97+i) ); } char ch; cout << "Enter a character: "; cin >> ch; map<char,int>::iterator iter = characterMap.find(ch); if( iter != characterMap.end() ) { cout << "ASCII value: " << iter->second << endl; } else { cout << "Character " << ch << " not found" << endl; }</pre> <hr> <h2><a name="get_allocator">get_allocator</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> allocator_type get_allocator();</pre> </td> </tr> </table> <p>The get_allocator() function returns the allocator of the map.</p> <hr> <h2><a name="insert">insert</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">iterator</a> insert( <a href="iterators.html">iterator</a> pos, const pair<<ahref="containers.html">KEY_TYPE</a>,<a href="containers.html">VALUE_TYPE</a>> &val ); void insert( <a href="iterators.html">input_iterator</a> start, <a href="iterators.html">input_iterator</a> end ); pair<iterator, bool> insert( const pair<<a href="containers.html">KEY_TYPE</a>,<a href="containers.html">VALUE_TYPE</a>> &val );</pre> </td> </tr> </table> <p>The function insert() either:</p> <ul> <li>inserts <i>val</i> after the element at <i>pos</i>, and returns an <a href= "iterators.html">iterator</a> to that element.</li> <li>inserts a range of elements from <i>start</i> to <i>end</i>.</li> <li>inserts <i>val</i>, but only if <i>val</i> doesn't already exist. The return value is an <a href= "iterators.html">iterator</a> to the element inserted, and a boolean describing whether an insertion took place.</li> </ul> <br> <br> <hr> <h2><a name="key_comp">key_comp</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> key_compare key_comp();</pre> </td> </tr> </table> <p>The function key_comp() returns the function that compares keys.</p> <hr> <h2><a name="lower_bound">lower_bound</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">iterator</a> lower_bound( const <a href="containers.html">KEY_TYPE</a> &key );</pre> </td> </tr> </table> <p>The lower_bound() function returns an <a href="iterators.html">iterator</a> to the first element greater than or equal to <i>key</i>.</p> <i>Related topics:</i><br> <strong><a href="#upper_bound">upper_bound()</a></strong> <hr> <h2><a name="max_size">max_size</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> size_type max_size();</pre> </td> </tr> </table> <p>The function max_size() returns the maximum number of elements that the map can hold.</p> <hr> <h2><a name="rbegin">rbegin</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">reverse_iterator</a> rbegin();</pre> </td> </tr> </table> <p>The rbegin() function returns a reverse <a href="iterators.html">iterator</a> to the end of the map.</p> <hr> <h2><a name="rend">rend</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">reverse_iterator</a> rend();</pre> </td> </tr> </table> <p>The function rend() returns a reverse <a href="iterators.html">iterator</a> to the start of the map.</p> <hr> <h2><a name="size">size</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> size_type size();</pre> </td> </tr> </table> <p>The function size() returns the number of elements currently in the map.</p> <hr> <h2><a name="swap">swap</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void swap( map<Key,T,Comp,Allocator> &obj );</pre> </td> </tr> </table> <p>The swap() function exchanges the elements in <i>obj</i> with those in the current map.</p> <hr> <h2><a name="upper_bound">upper_bound</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">iterator</a> upper_bound( const <a href="containers.html">KEY_TYPE</a> &key );</pre> </td> </tr> </table> <p>The function upper_bound() returns an <a href="iterators.html">iterator</a> to the first element in the map with a key greater than <i>key</i>.</p> <i>Related topics:</i><br> <strong><a href="#lower_bound">lower_bound()</a></strong> <hr> <h2><a name="value_comp">value_comp</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> value_compare value_comp();</pre> </td> </tr> </table> <p>The value_comp() function returns the function that compares values.</p> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -