📄 map_8018.htm
字号:
<UL><P>Erases all elements from the self.</P>
</UL>
<PRE>size_type
<B>count</B> (const key_type& x) const;</PRE>
<UL><P>Returns a 1 if a value with the key <SAMP>x</SAMP> exists in the map, otherwise returns a 0. </P>
</UL>
<PRE>bool
<B>empty</B>() const;</PRE>
<UL><P>Returns <SAMP>true</SAMP> if the map is empty, <SAMP>false</SAMP> otherwise.</P>
</UL>
<PRE>pair<iterator, iterator>
<B>equal_range</B> (const key_type& x);</PRE>
<UL><P>Returns the pair,<SAMP> (lower_bound(x), upper_bound(x))</SAMP>.</P>
</UL>
<PRE>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 position);</PRE>
<UL><P>Deletes the map element pointed to by the iterator <SAMP>position</SAMP>. Returns an iterator 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>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 map and last is reachable from first, all elements in the range (<SAMP>first, last</SAMP>) will be deleted from the map. Returns an iterator 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>size_type
<B>erase</B> (const key_type& x);</PRE>
<UL><P>Deletes the element with the key value <SAMP>x</SAMP> from the map, if one exists. Returns 1 if <SAMP>x</SAMP> existed in the map, 0 otherwise. </P>
</UL>
<PRE>iterator
<B>find</B> (const key_type& x);</PRE>
<UL><P>Searches the map 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 <SAMP>find</SAMP> above but returns a <SAMP>const_iterator</SAMP>.</P>
</UL>
<PRE>pair<iterator, bool>
<B>insert</B> (const value_type& x);
iterator
<B>insert</B> (iterator position, const value_type& x);</PRE>
<UL><P>If a <SAMP>value_type</SAMP> with the same key as <SAMP>x</SAMP> is not present in the map, then <SAMP>x</SAMP> is inserted into the map. Otherwise, the pair is not inserted. 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> which posess a unique key, one not already in the map, will be inserted into the map. The iterators <SAMP>first</SAMP> and <SAMP>last</SAMP> must return values of <SAMP>type</SAMP> <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 map. </P>
</UL>
<PRE>iterator
<B>lower_bound</B> (const key_type& x);</PRE>
<UL><P>Returns a reference to the first entry with a key greater than or equal to <SAMP>x</SAMP>.</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 map. This size is only constrained by the number of unique keys which can be represented by the type <SAMP>Key</SAMP>. </P>
</UL>
<PRE>size_type
<B>size</B>() const;</PRE>
<UL><P>Returns the number of elements in the map.</P>
</UL>
<PRE>void <B>swap</B> (map<Key, T, Compare, Allocator>& x);</PRE>
<UL><P>Swaps the contents of the map <SAMP>x</SAMP> with the current map, <SAMP>*this</SAMP>. </P>
</UL>
<PRE>iterator
<B>upper_bound</B> (const key_type& x);</PRE>
<UL><P>Returns a reference to the first entry with a key less than or equal to <SAMP>x</SAMP>.</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>pair<const Key, T></SAMP> values using the comparison operation,<SAMP> Compare</SAMP>, of the current map. This function is identical to <SAMP>key_comp</SAMP> for sets. </P>
</UL>
<A NAME="Non-member Operators"><H3>Non-member Operators</H3></A>
<PRE>template <class Key, class T, class Compare, class Allocator>
bool <B>operator==</B> (const map<Key, T, Compare, Allocator>& x,
const map<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>template <class Key, class T, class Compare, class Allocator>
bool <B>operator< </B>(const map<Key, T, Compare, Allocator>& x,
const map<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>(map<Key, T, Compare, Allocator>& a,
map<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>//
// map.cpp
//
#include <string>
#include <map>
#include <iostream.h>
typedef <B>map</B><string, int, less<string> > months_type;
// Print out a pair
template <class First, class Second>
ostream& operator<<(ostream& out,
const pair<First,Second> & p)
{
cout << p.first << " has " << p.second << " days";
return out;
}
// Print out a map
ostream& operator<<(ostream& out, const months_type & l)
{
copy(l.begin(),l.end(), ostream_iterator
<months_type::value_type>(cout,"\n"));
return out;
}
int main(void)
{
// create a map 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(string("January"), 31));
months.insert(value_type(string("Febuary"), 28));
months.insert(value_type(string("Febuary"), 29));
months.insert(value_type(string("March"), 31));
months.insert(value_type(string("April"), 30));
months.insert(value_type(string("May"), 31));
months.insert(value_type(string("June"), 30));
months.insert(value_type(string("July"), 31));
months.insert(value_type(string("August"), 31));
months.insert(value_type(string("September"), 30));
months.insert(value_type(string("October"), 31));
months.insert(value_type(string("November"), 30));
months.insert(value_type(string("December"), 31));
// print out the months
// Second Febuary is not present
cout << months << endl;
// Find the Number of days in June
months_type::iterator p = months.find(string("June"));
// print out the number of days in June
if (p != months.end())
cout << endl << *p << endl;
return 0;
}
Output :
April has 30 days
August has 31 days
December has 31 days
February has 28 days
January has 31 days
July has 31 days
June has 30 days
March has 31 days
May has 31 days
November has 30 days
October has 31 days
September has 30 days
</PRE>
<A NAME="Warning"><H3>Warning</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 <SAMP>map<Key,T,Compare,Allocator></SAMP> that takes two templated iterators:</P>
<PRE></PRE>
<PRE>template <class InputIterator>
map (InputIterator, InputIterator, const Compare& = Compare(),
const Allocator& = Allocator());
</PRE><P><B><I>map</B></I> also has an insert 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 <B><I>map</B></I> in the following two ways: </P>
<PRE>map<int, int, less<int> >::value_type intarray[10];
map<int, int, less<int> > first_map(intarray, intarray + 10);
map<int, int, less<int> > second_map(first_map.begin(),
first_map.end());
</PRE><P>But not this way:</P>
<PRE>map<long, long, less<long> > long_map(first_map.begin(),
first_map.end());</PRE>
<P>Since the <SAMP>long_map</SAMP> and <SAMP>first_map</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>map<int, int, less<int>, allocator></PRE>
<PRE></PRE><P>instead of:</P>
<PRE>map<int, int></PRE>
<PRE></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="mul_8396.htm"><B><I>multimap</B></I></A></P>
<HR>
<A HREF="mak_0285.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="max_6671.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -