📄 multimap_constructors.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>Multimap constructors & destructors</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++ Multimaps</a> > <a href= "multimap_constructors.html">Multimap constructors & destructors</a> </div> <div class="name-format"> Multimap constructors & destructors </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <map> multimap(); multimap( const multimap& c ); multimap( iterator begin, iterator end, const key_compare& cmp = Compare(), const allocator& alloc = Allocator() ); ~multimap();</pre> <p>Multimaps have several constructors:</p> <ul> <li>The default constructor takes no arguments, creates a new instance of that multimap, and runs in <a href="../complexity.html">constant time</a>.</li> <li>The default copy constructor runs in <a href= "../complexity.html">linear time</a> and can be used to create a new multimap that is a copy of the given multimap <em>c</em>.</li> <li>Multimaps can also be created from a range of elements defined by <em>begin</em> and <em>end</em>. When using this constructor, an optional comparison function <em>cmp</em> and allocator <em>alloc</em> can also be provided.</li> </ul> <p>The default destructor is called when the multimap should be destroyed.</p> <p>The template definition of multimaps requires that both a key type and value type be supplied. For example, you can instantiate a multimap that maps strings to integers with this statement:</p> <pre class="example-code"> multimap<string,int> m;</pre> <p>You can also supply a comparison function and an allocator in the template:</p> <pre class="example-code"> multimap<string,int,myComp,myAlloc> m;</pre> <p>For example, the following code uses a multimap to associate a series of employee names with numerical IDs:</p> <pre class="example-code"> multimap<string,int> m; int employeeID = 0; m.insert( pair<string,int>("Bob Smith",employeeID++) ); m.insert( pair<string,int>("Bob Thompson",employeeID++) ); m.insert( pair<string,int>("Bob Smithey",employeeID++) ); m.insert( pair<string,int>("Bob Smith",employeeID++) ); cout << "Number of employees named 'Bob Smith': " << m.count("Bob Smith") << endl; cout << "Number of employees named 'Bob Thompson': " << m.count("Bob Thompson") << endl; cout << "Number of employees named 'Bob Smithey': " << m.count("Bob Smithey") << endl; cout << "Employee list: " << endl; for( multimap<string, int>::iterator iter = m.begin(); iter != m.end(); ++iter ) { cout << " Name: " << iter->first << ", ID #" << iter->second << endl; }</pre> <p>When run, the above code produces the following output. Note that the employee list is displayed in alphabetical order, because multimaps are sorted associative containers:</p> <pre class="example-code"> Number of employees named 'Bob Smith': 2 Number of employees named 'Bob Thompson': 1 Number of employees named 'Bob Smithey': 1 Employee list: Name: Bob Smith, ID #0 Name: Bob Smith, ID #3 Name: Bob Smithey, ID #2 Name: Bob Thompson, ID #1</pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="count.html">count</a><br> <a href="insert.html">insert</a> </div> </div> </td> </tr> </table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -