📄 mul_8396.htm
字号:
<PRE>pair<iterator,iterator>
<B>equal_range </B>(const key_type& x);
pair<const_iterator,const_iterator>
<B>equal_range</B> (const key_type& x) const;</PRE>
<UL><P>Returns the pair<SAMP> (lower_bound(x), upper_bound(x))</SAMP>.</P>
</UL>
<PRE>iterator
<B>erase</B> (iterator first, iterator last);</PRE>
<UL><P>Providing the iterators <SAMP>first</SAMP> and <SAMP>last</SAMP> point to the same multimap and last is reachable from first, all elements in the range (<SAMP>first, last</SAMP>) will be deleted from the multimap. Returns an <SAMP>iterator</SAMP> pointing to the element following the last deleted element, or <SAMP>end(),</SAMP> if there were no elements after the deleted range.</P>
</UL>
<PRE>iterator
<B>erase</B> (iterator position);</PRE>
<UL><P>Deletes the multimap element pointed to by the iterator <SAMP>position</SAMP>. Returns an <SAMP>iterator</SAMP> pointing to the element following the deleted element, or <SAMP>end(), </SAMP>if the deleted item was the last one in this list.</P>
</UL>
<PRE>size_type
<B>erase</B> (const key_type& x);</PRE>
<UL><P>Deletes the elements with the key value <SAMP>x</SAMP> from the map, if any exist. Returns the number of deleted elements, or 0 otherwise. </P>
</UL>
<PRE>iterator
<B>find</B> (const key_type& x);</PRE>
<UL><P>Searches the multimap for a pair with the key value <SAMP>x</SAMP> and returns an <SAMP>iterator</SAMP> to that pair if it is found. If such a pair is not found the value <SAMP>end() </SAMP> is returned.</P>
</UL>
<PRE>const_iterator
<B>find</B> (const key_type& x) const;</PRE>
<UL><P>Same as find above but returns a <SAMP>const_iterator</SAMP>.</P>
</UL>
<PRE>iterator
<B>insert</B> (const value_type& x);
iterator
<B>insert</B> (iterator position, const value_type& x);</PRE>
<UL><P><SAMP>x</SAMP> is inserted into the multimap. A position may be supplied as a hint regarding where to do the insertion. If the insertion may be done right after <SAMP>position</SAMP> then it takes amortized constant time. Otherwise it will take <SAMP>O(log N)</SAMP> time.</P>
</UL>
<PRE>template <class InputIterator>
void
<B>insert</B> (InputIterator first, InputIterator last);</PRE>
<UL><P>Copies of each element in the range <SAMP>[first, last)</SAMP> will be inserted into the multimap. The iterators <SAMP>first</SAMP> and <SAMP>last</SAMP> must return values of type <SAMP>pair<T1,T2></SAMP>. This operation takes approximately <SAMP>O(N*log(size()+N))</SAMP> time.</P>
</UL>
<PRE>key_compare
<B>key_comp</B>() const;</PRE>
<UL><P>Returns a function object capable of comparing key values using the comparison operation, <SAMP>Compare</SAMP>, of the current multimap.</P>
</UL>
<PRE>iterator
<B>lower_bound </B>(const key_type& x);</PRE>
<UL><P>Returns an <SAMP>iterator</SAMP> to the first multimap element whose key is greater than or equal to <SAMP>x</SAMP>. If no such element exists then <SAMP>end()</SAMP> is returned.</P>
</UL>
<PRE>const_iterator
<B>lower_bound </B>(const key_type& x) const;</PRE>
<UL><P>Same as <SAMP>lower_bound</SAMP> above but returns a <SAMP>const_iterator.</SAMP></P>
</UL>
<PRE>size_type
<B>max_size</B>() const;</PRE>
<UL><P>Returns the maximum possible size of the multimap.</P>
</UL>
<PRE>size_type
<B>size</B>() const;</PRE>
<UL><P>Returns the number of elements in the multimap.</P>
</UL>
<PRE>void
<B>swap</B> (multimap<Key, T, Compare, Allocator>& x);</PRE>
<UL><P>Swaps the contents of the multimap <SAMP>x </SAMP>with the current multimap, <SAMP>*this</SAMP>.</P>
</UL>
<PRE>iterator
<B>upper_bound</B> (const key_type& x);</PRE>
<UL><P>Returns an <SAMP>iterator</SAMP> to the first element whose key is less than or equal to <SAMP>x</SAMP>. If no such element exists, then <SAMP>end()</SAMP> is returned.</P>
</UL>
<PRE>const_iterator
<B>upper_bound </B>(const key_type& x) const;</PRE>
<UL><P>Same as <SAMP>upper_bound</SAMP> above but returns a <SAMP>const_iterator</SAMP>.</P>
</UL>
<PRE>value_compare
<B>value_comp</B>() const;</PRE>
<UL><P>Returns a function object capable of comparing <SAMP>value_types</SAMP> (<SAMP>key,value</SAMP> pairs) using the comparison operation, <SAMP>Compare</SAMP>, of the current multimap.</P>
</UL>
<A NAME="Non-member Operators"><H3>Non-member Operators</H3></A>
<PRE>bool
<B>operator==</B>(const multimap<Key, T, Compare, Allocator>& x,
const multimap<Key, T, Compare, Allocator>& y);</PRE>
<UL><P>Returns <SAMP>true</SAMP> if all elements in <SAMP>x</SAMP> are element-wise equal to all elements in <SAMP>y</SAMP>, using<SAMP> (T::operator==). </SAMP>Otherwise it returns <SAMP>false</SAMP>.</P>
</UL>
<PRE>bool
<B>operator<</B> (const multimap<Key, T, Compare, Allocator>& x,
const multimap<Key, T, Compare, Allocator>& y);</PRE>
<UL><P>Returns <SAMP>true</SAMP> if <SAMP>x</SAMP> is lexicographically less than <SAMP>y</SAMP>. Otherwise, it returns <SAMP>false</SAMP>.</P>
</UL>
<PRE>template<class Key, class T, class Compare, class Allocator>
void <B>swap</B> (multimap<Key, T, Compare, Allocator>& a,
multimap<Key, T, Compare, Allocator>& b);</PRE>
<UL><P>Efficiently swaps the contents of <SAMP>a</SAMP> and <SAMP>b</SAMP>.</P>
</UL>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// multimap.cpp
//
#include <string>
#include <map>
#include <iostream.h>
typedef <B>multimap</B><int, string, less<int> > months_type;
// Print out a pair
template <class First, class Second>
ostream& operator<<(ostream& out,
const pair<First,Second>& p)
{
cout << p.second << " has " << p.first << " days";
return out;
}
// Print out a multimap
ostream& operator<<(ostream& out, months_type l)
{
copy(l.begin(),l.end(), ostream_iterator
<months_type::value_type>(cout,"\n"));
return out;
}
int main(void)
{
// create a multimap of months and the number of
// days in the month
months_type months;
typedef months_type::value_type value_type;
// Put the months in the multimap
months.insert(value_type(31, string("January")));
months.insert(value_type(28, string("Febuary")));
months.insert(value_type(31, string("March")));
months.insert(value_type(30, string("April")));
months.insert(value_type(31, string("May")));
months.insert(value_type(30, string("June")));
months.insert(value_type(31, string("July")));
months.insert(value_type(31, string("August")));
months.insert(value_type(30, string("September")));
months.insert(value_type(31, string("October")));
months.insert(value_type(30, string("November")));
months.insert(value_type(31, string("December")));
// print out the months
cout << "All months of the year" << endl << months << endl;
// Find the Months with 30 days
pair<months_type::iterator,months_type::iterator> p =
months.equal_range(30);
// print out the 30 day months
cout << endl << "Months with 30 days" << endl;
copy(p.first,p.second,
ostream_iterator<months_type::value_type>(cout,"\n"));
return 0;
}
Output :
All months of the year
February has 28 days
April has 30 days
June has 30 days
September has 30 days
November has 30 days
January has 31 days
March has 31 days
May has 31 days
July has 31 days
August has 31 days
October has 31 days
December has 31 days
Months with 30 days
April has 30 days
June has 30 days
September has 30 days
November has 30 days
</PRE>
<A NAME="Warnings"><H3>Warnings</H3></A>
<P>Member function templates are used in all containers provided by the Standard Template Library. An example of this feature is the constructor for <B><I>multimap<Key,T,Compare,Allocator></B></I> that takes two templated iterators:</P>
<PRE></PRE>
<PRE>template <class InputIterator>
multimap (InputIterator, InputIterator,
const Compare& = Compare(),
const Allocator& = Allocator());
</PRE><P><B><I>multimap</B></I> also has an <SAMP>insert</SAMP> function of this type. These functions, when not restricted by compiler limitations, allow you to use any type of input iterator as arguments. For compilers that do not support this feature we provide substitute functions that allow you to use an iterator obtained from the same type of container as the one you are constructing (or calling a member function on), or you can use a pointer to the type of element you have in the container.</P>
<P>For example, if your compiler does not support member function templates you can construct a multimap in the following two ways: </P>
<PRE>multimap<int, int, less<int>, allocator>::value_type intarray[10];
multimap<int, int, less<int>, allocator> first_map(intarry,
intarray + 10);
multimap<int, int, less<int>, allocator>
second_multimap(first_multimap.begin(), first_multimap.end());
</PRE>
<P>but not this way:</P>
<PRE>multimap<long, long, less<long>, allocator>
long_multimap(first_multimap.begin(),first_multimap.end());
</PRE><P>since the <SAMP>long_multimap</SAMP> and <SAMP>first_multimap </SAMP>are not the same type.</P>
<P>Also, many compilers do not support default template arguments. If your compiler is one of these you need to always supply the <SAMP>Compare</SAMP> template argument and the <SAMP>Allocator</SAMP> template argument. For instance you'll have to write:</P>
<PRE>multimap<int, int, less<int>, allocator></PRE>
<P>instead of:</P>
<PRE>multimap<int, int></PRE>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="all_7029.htm"><B><I>allocator</B></I></A>, <A HREF="Con_2487.htm"><B><I>Containers</B></I></A>, <A HREF="Ite_5295.htm"><B><I>Iterators</B></I></A>, <A HREF="map_8018.htm"><B><I>map</B></I></A></P>
<HR>
<A HREF="mod_6765.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="mul_0958.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -