c++ Muitmaps

Muitmaps 是存储由密钥值和映射值的组合构成的元素,下列特定的顺序关联容器,以及其中多个元件可具有等价密钥。
在一个multimap中,关键值一般用于排序和唯一标识的元素,而映射的值存储关联于该键的内容。类型的键和映射值可能不同,并且在构件的类型的value_type,这是一对型组合都被分组在一起:
ms-help://MS.MSDNQTR.v90.chs/dv_vcstdlib/html/0e9125b6-620c-4f68-9f91-3b5d6ca509d0.htm
http://www.cplusplus.com/reference/map/multimap/?kw=multimap

begin	返回迭代开始
end	返回迭代结束
rbegin	返回反向迭代器反向开始
rend	返回反向迭代器反向端
cbegin  返回的const_iterator开头
cend 	返回的const_iterator结束
crbegin 返回的const_reverse_iterator反转开始
crend	返回的const_reverse_iterator反向端 

empty	测试容器是否是空的
size	返回容器的大小
max_size返回最大尺寸	

insert	插入元素
erase	删除元素
swap	交换的内容
clear	清除内容
emplace 构建并插入元素
emplace_hint 	构建并插入提示元素

key_comp返回键比较对象
value_comp返回值比较对象

find	获得迭代器元素
count	算上与特定的要素
lower_bound	返回迭代器下限
upper_bound	返回迭代上限
equal_range	获得平等的要素的范围

get_allocator	获得分配
 
 
 
 
<pre name="code" class="cpp">#include <map>
#include <iostream>
#include <xfunctional>

//构造函数
void multimapConstructor(void);

//返回一个迭代解决multimap中的第一个元素
void multimap_begin(void);

//擦除一个multimap中的所有元素
void multimap_clear(void);

//返回multimap中的,其关键的参数 - 指定键匹配元件的数目
void multimap_count(void);

//返回multimap是否为空
void mulitmap_empty(void);

//返回地址multimap中取得成功的最后一个元素的位置的迭代器
void mulitmap_end(void);

//发现的元素,其中的元素的键相匹配的指定值的范围内
void mulitmap_equal_range(void);

//删除元素或一个范围内指定位置multimap中的元素或删除匹配指定键的元素
void muiltmap_erase(void);

//返回迭代寻址在具有关键相当于一个指定键的multimap中一个元件的第一位置
void muiltmap_find(void);

//返回用于构造multimap中的分配对象的副本
void muiltmap_get_allocator(void);

//插入一个元素或一定范围内的元素到multimap中的
void muiltmap_insert(void);

//检索用于multimap中的命令的键的比较对象的副本
void muiltmap_key_comp(void);

//返回一个迭代到第一元件multimap中的,与一个键比指定的键等于或大于
void muiltmap_lower_bound(void);

//返回multimap中的最大长度
void muiltmap_max_size(void);

//返回一个迭代解决的第一个元素在逆转多重映射
void muiltmap_rbegin(void);

//返回解决了一个逆转多重映射成功的最后一个元素的位置的迭代器
void muiltmap_rend(void);

//返回multimap中元素的数量
void muiltmap_size(void);

//交换两个muiltmap的元素
void muiltmap_swap(void);

//返回一个迭代器的第一个元素multimap中以关键比指定的键更大
void muiltmap_upper_bound(void);

//成员函数返回一个功能对象,通过比较它们的键值确定multimap中的元素的顺序
void muiltmap_value_comp(void);

int main()
{
	//multimapConstructor();
	//multimap_begin();
	//multimap_clear();
	//multimap_count();
	//mulitmap_empty();
	//mulitmap_end();
	//mulitmap_equal_range();
	//muiltmap_erase();
	//muiltmap_find();
	//muiltmap_get_allocator();
	//muiltmap_insert();
	//muiltmap_key_comp();
	//muiltmap_lower_bound();
	//muiltmap_max_size();
	//muiltmap_rbegin();
	//muiltmap_rend();
	//muiltmap_size();
	//muiltmap_swap();
	//muiltmap_upper_bound();
	muiltmap_value_comp();
	return 0;
}

//构造函数
void multimapConstructor(void)
{
	using namespace std;
	typedef pair <int, int> Int_Pair;
	multimap <int, int>::iterator m1_Iter, m3_Iter, m4_Iter, m5_Iter, m6_Iter;
	multimap <int, int, greater<int> >::iterator m2_Iter;

	// Create an empty multimap m0 of key type integer
	multimap <int, int> m0;

	// Create an empty multimap m1 with the key comparison
	// function of less than, then insert 4 elements
	multimap <int, int, less<int> > m1;
	m1.insert(Int_Pair(1, 10));
	m1.insert(Int_Pair(2, 20));
	m1.insert(Int_Pair(3, 30));
	m1.insert(Int_Pair(4, 40));

	// Create an empty multimap m2 with the key comparison
	// function of geater than, then insert 2 elements
	multimap <int, int, greater<int> > m2;
	m2.insert(Int_Pair(1, 10));
	m2.insert(Int_Pair(2, 20));

	// Create a multimap m3 with the 
	// allocator of multimap m1
	multimap <int, int>::allocator_type m1_Alloc;
	m1_Alloc = m1.get_allocator();
	multimap <int, int> m3(less<int>(), m1_Alloc);
	m3.insert(Int_Pair(3, 30));

	// Create a copy, multimap m4, of multimap m1
	multimap <int, int> m4(m1);

	// Create a multimap m5 by copying the range m1[_First, _Last)
	multimap <int, int>::const_iterator m1_bcIter, m1_ecIter;
	m1_bcIter = m1.begin();
	m1_ecIter = m1.begin();
	m1_ecIter++;
	m1_ecIter++;
	multimap <int, int> m5(m1_bcIter, m1_ecIter);

	// Create a multimap m6 by copying the range m4[_First, _Last)
	// and with the allocator of multimap m2
	multimap <int, int>::allocator_type m2_Alloc;
	m2_Alloc = m2.get_allocator();
	multimap <int, int> m6(m4.begin(), ++m4.begin(), less<int>(), m2_Alloc);

	cout << "m1 =";
	for (m1_Iter = m1.begin(); m1_Iter != m1.end(); m1_Iter++)
		cout << " " << m1_Iter->second;
	cout << endl;

	cout << "m2 =";
	for (m2_Iter = m2.begin(); m2_Iter != m2.end(); m2_Iter++)
		cout << " " << m2_Iter->second;
	cout << endl;

	cout << "m3 =";
	for (m3_Iter = m3.begin(); m3_Iter != m3.end(); m3_Iter++)
		cout << " " << m3_Iter->second;
	cout << endl;

	cout << "m4 =";
	for (m4_Iter = m4.begin(); m4_Iter != m4.end(); m4_Iter++)
		cout << " " << m4_Iter->second;
	cout << endl;

	cout << "m5 =";
	for (m5_Iter = m5.begin(); m5_Iter != m5.end(); m5_Iter++)
		cout << " " << m5_Iter->second;
	cout << endl;

	cout << "m6 =";
	for (m6_Iter = m6.begin(); m6_Iter != m6.end(); m6_Iter++)
		cout << " " << m6_Iter->second;
	cout << endl;

	return;
	/*
	m1 = 10 20 30 40
	m2 = 20 10
	m3 = 30
	m4 = 10 20 30 40
	m5 = 10 20
	m6 = 10
	请按任意键继续. . .
	*/
}

//返回一个迭代解决multimap中的第一个元素
void multimap_begin()
{
	using namespace std;
	multimap <int, int> m1;

	multimap <int, int> ::iterator m1_Iter;
	multimap <int, int> ::const_iterator m1_cIter;
	typedef pair <int, int> Int_Pair;

	m1.insert(Int_Pair(0, 0));
	m1.insert(Int_Pair(1, 1));
	m1.insert(Int_Pair(2, 4));

	m1_cIter = m1.begin();
	cout << "The first element of m1 is " << m1_cIter->first << endl;

	m1_Iter = m1.begin();
	m1.erase(m1_Iter);

	// The following 2 lines would err as the iterator is const
	// m1_cIter = m1.begin ( );
	// m1.erase ( m1_cIter );

	m1_cIter = m1.begin();
	cout << "First element of m1 is now " << m1_cIter->first << endl;


	return;
	/*
	The first element of m1 is 0
	First element of m1 is now 1
	请按任意键继续. . .
	*/
}

//擦除一个multimap中的所有元素
void multimap_clear(void)
{
	using namespace std;
	multimap<int, int> m1;
	multimap<int, int>::size_type i;
	typedef pair<int, int> Int_Pair;

	m1.insert(Int_Pair(1, 1));
	m1.insert(Int_Pair(2, 4));

	i = m1.size();
	cout << "The size of the multimap is initially "
		<< i << "." << endl;

	m1.clear();
	i = m1.size();
	cout << "The size of the multimap after clearing is "
		<< i << "." << endl;

	return;
	/*
	The size of the multimap is initially 2.
	The size of the multimap after clearing is 0.
	请按任意键继续. . .
	*/
}

//返回multimap中的,其关键的参数 - 指定键匹配元件的数目
void multimap_count(void)
{
	using namespace std;
	multimap<int, int> m1;
	multimap<int, int>::size_type i;
	typedef pair<int, int> Int_Pair;

	m1.insert(Int_Pair(1, 1));
	m1.insert(Int_Pair(2, 1));
	m1.insert(Int_Pair(1, 4));
	m1.insert(Int_Pair(2, 1));

	// Elements do not need to have unique keys in multimap,
	// so duplicates are allowed and counted
	i = m1.count(1);
	cout << "The number of elements in m1 with a sort key of 1 is: "
		<< i << "." << endl;

	i = m1.count(2);
	cout << "The number of elements in m1 with a sort key of 2 is: "
		<< i << "." << endl;

	i = m1.count(3);
	cout << "The number of elements in m1 with a sort key of 3 is: "
		<< i << "." << endl;

	return;
	/*
	The number of elements in m1 with a sort key of 1 is: 2.
	The number of elements in m1 with a sort key of 2 is: 2.
	The number of elements in m1 with a sort key of 3 is: 0.
	请按任意键继续. . .
	*/
}

//返回multimap是否为空
void mulitmap_empty(void)
{
	using namespace std;
	multimap <int, int> m1, m2;

	typedef pair <int, int> Int_Pair;
	m1.insert(Int_Pair(1, 1));

	if (m1.empty())
		cout << "The multimap m1 is empty." << endl;
	else
		cout << "The multimap m1 is not empty." << endl;

	if (m2.empty())
		cout << "The multimap m2 is empty." << endl;
	else
		cout << "The multimap m2 is not empty." << endl;

	return;
	/*
	The multimap m1 is not empty.
	The multimap m2 is empty.
	请按任意键继续. . .
	*/
}

//返回地址multimap中取得成功的最后一个元素的位置的迭代器
void mulitmap_end(void)
{
	using namespace std;
	multimap <int, int> m1;

	multimap <int, int> ::iterator m1_Iter;
	multimap <int, int> ::const_iterator m1_cIter;
	typedef pair <int, int> Int_Pair;

	m1.insert(Int_Pair(1, 10));
	m1.insert(Int_Pair(2, 20));
	m1.insert(Int_Pair(3, 30));

	m1_cIter = m1.end();
	m1_cIter--;
	cout << "The value of last element of m1 is "
		<< m1_cIter->second << endl;

	m1_Iter = m1.end();
	m1_Iter--;
	m1.erase(m1_Iter);

	// The following 2 lines would err because the iterator is const
	// m1_cIter = m1.end ( );
	// m1_cIter--;
	// m1.erase ( m1_cIter );

	m1_cIter = m1.end();
	m1_cIter--;
	cout << "The value of last element of m1 is now "
		<< m1_cIter->second << endl;

	return;
	/*
	The value of last element of m1 is 30
	The value of last element of m1 is now 20
	请按任意键继续. . .
	*/
}

//发现的元素,其中的元素的键相匹配的指定值的范围内
void mulitmap_equal_range(void)
{
	using namespace std;
	typedef multimap <int, int, less<int> > IntMMap;
	IntMMap m1;
	multimap <int, int> ::const_iterator m1_RcIter;
	typedef pair <int, int> Int_Pair;

	m1.insert(Int_Pair(1, 10));
	m1.insert(Int_Pair(2, 20));
	m1.insert(Int_Pair(3, 30));

	pair <IntMMap::const_iterator, IntMMap::const_iterator> p1, p2;
	p1 = m1.equal_range(2);

	cout << "The lower bound of the element with "
		<< "a key of 2 in the multimap m1 is: "
		<< p1.first->second << "." << endl;

	cout << "The upper bound of the element with "
		<< "a key of 2 in the multimap m1 is: "
		<< p1.second->second << "." << endl;

	// Compare the upper_bound called directly 
	m1_RcIter = m1.upper_bound(2);

	cout << "A direct call of upper_bound( 2 ) gives "
		<< m1_RcIter->second << "," << endl
		<< " matching the 2nd element of the pair"
		<< " returned by equal_range( 2 )." << endl;

	p2 = m1.equal_range(4);

	// If no match is found for the key,
	// both elements of the pair return end( )
	if ((p2.first == m1.end()) && (p2.second == m1.end()))
		cout << "The multimap m1 doesn't have an element "
		<< "with a key less than 4." << endl;
	else
		cout << "The element of multimap m1 with a key >= 40 is: "
		<< p1.first->first << "." << endl;

	return;
	/*
	The lower bound of the element with a key of 2 in the multimap m1 is: 20.
	The upper bound of the element with a key of 2 in the multimap m1 is: 30.
	A direct call of upper_bound( 2 ) gives 30,
	matching the 2nd element of the pair returned by equal_range( 2 ).
	The multimap m1 doesn't have an element with a key less than 4.
	请按任意键继续. . .
	*/
}

//删除元素或一个范围内指定位置multimap中的元素或删除匹配指定键的元素
void muiltmap_erase(void)
{
	using namespace std;
	multimap<int, int> m1, m2, m3;
	multimap<int, int> ::iterator pIter, Iter1, Iter2;
	int i;
	multimap<int, int>::size_type n;
	typedef pair<int, int> Int_Pair;

	for (i = 1; i < 5; i++)
	{
		m1.insert(Int_Pair(i, i));
		m2.insert(Int_Pair(i, i*i));
		m3.insert(Int_Pair(i, i - 1));
	}

	// The 1st member function removes an element at a given position
	Iter1 = ++m1.begin();
	m1.erase(Iter1);

	cout << "After the 2nd element is deleted, "
		<< "the multimap ms1 is:";
	for (pIter = m1.begin(); pIter != m1.end(); pIter++)
		cout << " " << pIter->second;
	cout << "." << endl;

	// The 2nd member function removes elements
	// in the range [_First, _Last)
	Iter1 = ++m2.begin();
	Iter2 = --m2.end();
	m2.erase(Iter1, Iter2);

	cout << "After the middle two elements are deleted, "
		<< "the multimap m2 is:";
	for (pIter = m2.begin(); pIter != m2.end(); pIter++)
		cout << " " << pIter->second;
	cout << "." << endl;

	// The 3rd member function removes elements with a given _Key
	m3.insert(Int_Pair(2, 5));
	n = m3.erase(2);

	cout << "After the element with a key of 2 is deleted,\n"
		<< "the multimap m3 is:";
	for (pIter = m3.begin(); pIter != m3.end(); pIter++)
		cout << " " << pIter->second;
	cout << "." << endl;

	// The 3rd member function returns the number of elements removed
	cout << "The number of elements removed from m3 is: "
		<< n << "." << endl;

	// The dereferenced iterator can also be used to specify a key
	Iter1 = ++m3.begin();
	m3.erase(Iter1);

	cout << "After another element with a key equal to that"
		<< endl;
	cout << "of the 2nd element is deleted, "
		<< "the multimap m3 is:";
	for (pIter = m3.begin(); pIter != m3.end(); pIter++)
		cout << " " << pIter->second;
	cout << "." << endl;

	return;
	/*
	After the 2nd element is deleted, the multimap ms1 is: 1 3 4.
	After the middle two elements are deleted, the multimap m2 is: 1 16.
	After the element with a key of 2 is deleted,
	the multimap m3 is: 0 2 3.
	The number of elements removed from m3 is: 2.
	After another element with a key equal to that
	of the 2nd element is deleted, the multimap m3 is: 0 3.
	请按任意键继续. . .
	*/
}

//返回迭代寻址在具有关键相当于一个指定键的multimap中一个元件的第一位置
void muiltmap_find(void)
{
	using namespace std;
	multimap <int, int> m1;
	multimap <int, int> ::const_iterator m1_AcIter, m1_RcIter;
	typedef pair <int, int> Int_Pair;

	m1.insert(Int_Pair(1, 10));
	m1.insert(Int_Pair(2, 20));
	m1.insert(Int_Pair(3, 20));
	m1.insert(Int_Pair(3, 30));

	m1_RcIter = m1.find(2);
	cout << "The element of multimap m1 with a key of 2 is: "
		<< m1_RcIter->second << "." << endl;

	m1_RcIter = m1.find(3);
	cout << "The first element of multimap m1 with a key of 3 is: "
		<< m1_RcIter->second << "." << endl;

	// If no match is found for the key, end( ) is returned
	m1_RcIter = m1.find(4);

	if (m1_RcIter == m1.end())
		cout << "The multimap m1 doesn't have an element "
		<< "with a key of 4." << endl;
	else
		cout << "The element of multimap m1 with a key of 4 is: "
		<< m1_RcIter->second << "." << endl;

	// The element at a specific location in the multimap can be
	// found using a dereferenced iterator addressing the location
	m1_AcIter = m1.end();
	m1_AcIter--;
	m1_RcIter = m1.find(m1_AcIter->first);
	cout << "The first element of m1 with a key matching"
		<< endl << "that of the last element is: "
		<< m1_RcIter->second << "." << endl;

	// Note that the first element with a key equal to
	// the key of the last element is not the last element
	if (m1_RcIter == --m1.end())
		cout << "This is the last element of multimap m1."
		<< endl;
	else
		cout << "This is not the last element of multimap m1."
		<< endl;

	return;
	/*
	The element of multimap m1 with a key of 2 is: 20.
	The first element of multimap m1 with a key of 3 is: 20.
	The multimap m1 doesn't have an element with a key of 4.
	The first element of m1 with a key matching
	that of the last element is: 20.
	This is not the last element of multimap m1.
	请按任意键继续. . .
	*/
}

//返回用于构造multimap中的分配对象的副本
void muiltmap_get_allocator(void)
{
	using namespace std;
	multimap <int, int>::allocator_type m1_Alloc;
	multimap <int, int>::allocator_type m2_Alloc;
	multimap <int, double>::allocator_type m3_Alloc;
	multimap <int, int>::allocator_type m4_Alloc;

	// The following lines declare objects
	// that use the default allocator.
	multimap <int, int> m1;
	multimap <int, int, allocator<int> > m2;
	multimap <int, double, allocator<double> > m3;

	m1_Alloc = m1.get_allocator();
	m2_Alloc = m2.get_allocator();
	m3_Alloc = m3.get_allocator();

	cout << "The number of integers that can be allocated"
		<< endl << "before free memory is exhausted: "
		<< m2.max_size() << ".\n" << endl;

	cout << "The number of doubles that can be allocated"
		<< endl << "before free memory is exhausted: "
		<< m3.max_size() << ".\n" << endl;

	// The following line creates a multimap m4
	// with the allocator of multimap m1.
	map <int, int> m4(less<int>(), m1_Alloc);

	m4_Alloc = m4.get_allocator();

	// Two allocators are interchangeable if
	// storage allocated from each can be
	// deallocated via the other
	if (m1_Alloc == m4_Alloc)
	{
		cout << "The allocators are interchangeable."
			<< endl;
	}
	else
	{
		cout << "The allocators are not interchangeable."
			<< endl;
	}

	return;
	/*
	The number of integers that can be allocated
	before free memory is exhausted: 178956970.

	The number of doubles that can be allocated
	before free memory is exhausted: 134217727.

	The allocators are interchangeable.
	请按任意键继续. . .
	*/
}

//插入一个元素或一定范围内的元素到multimap中的
void muiltmap_insert(void)
{
	using namespace std;
	multimap <int, int>::iterator m1_pIter, m2_pIter;

	multimap <int, int> m1, m2;
	typedef pair <int, int> Int_Pair;

	m1.insert(Int_Pair(1, 10));
	m1.insert(Int_Pair(2, 20));
	m1.insert(Int_Pair(3, 30));

	cout << "The original key values of m1 =";
	for (m1_pIter = m1.begin(); m1_pIter != m1.end(); m1_pIter++)
		cout << " " << m1_pIter->first;
	cout << "." << endl;

	cout << "The original mapped values of m1 =";
	for (m1_pIter = m1.begin(); m1_pIter != m1.end(); m1_pIter++)
		cout << " " << m1_pIter->second;
	cout << "." << endl;

	m1.insert(Int_Pair(1, 10));

	// The hint version of insert
	m1.insert(--m1.end(), Int_Pair(4, 40));

	cout << "After the insertions, the key values of m1 =";
	for (m1_pIter = m1.begin(); m1_pIter != m1.end(); m1_pIter++)
		cout << " " << m1_pIter->first;
	cout << "," << endl;

	cout << " and the mapped values of m1 =";
	for (m1_pIter = m1.begin(); m1_pIter != m1.end(); m1_pIter++)
		cout << " " << m1_pIter->second;
	cout << "." << endl;

	m2.insert(Int_Pair(10, 100));

	// The templatized version inserting a range
	m2.insert(++m1.begin(), --m1.end());

	cout << "After the insertions, the key values of m2 =";
	for (m2_pIter = m2.begin(); m2_pIter != m2.end(); m2_pIter++)
		cout << " " << m2_pIter->first;
	cout << "," << endl;

	cout << " and the mapped values of m2 =";
	for (m2_pIter = m2.begin(); m2_pIter != m2.end(); m2_pIter++)
		cout << " " << m2_pIter->second;
	cout << "." << endl;

	return;
	/*
	The original key values of m1 = 1 2 3.
	The original mapped values of m1 = 10 20 30.
	After the insertions, the key values of m1 = 1 1 2 3 4,
	and the mapped values of m1 = 10 10 20 30 40.
	After the insertions, the key values of m2 = 1 2 3 10,
	and the mapped values of m2 = 10 20 30 100.
	请按任意键继续. . .
	*/
}

//检索用于multimap中的命令的键的比较对象的副本
void muiltmap_key_comp(void)
{
	using namespace std;

	multimap <int, int, less<int> > m1;
	multimap <int, int, less<int> >::key_compare kc1 = m1.key_comp();
	bool result1 = kc1(2, 3);
	if (result1 == true)
	{
		cout << "kc1( 2,3 ) returns value of true, "
			<< "where kc1 is the function object of m1."
			<< endl;
	}
	else
	{
		cout << "kc1( 2,3 ) returns value of false "
			<< "where kc1 is the function object of m1."
			<< endl;
	}

	multimap <int, int, greater<int> > m2;
	multimap <int, int, greater<int> >::key_compare kc2 = m2.key_comp();
	bool result2 = kc2(2, 3);
	if (result2 == true)
	{
		cout << "kc2( 2,3 ) returns value of true, "
			<< "where kc2 is the function object of m2."
			<< endl;
	}
	else
	{
		cout << "kc2( 2,3 ) returns value of false, "
			<< "where kc2 is the function object of m2."
			<< endl;
	}

	return;
	/*
	kc1( 2,3 ) returns value of true, where kc1 is the function object of m1.
	kc2( 2,3 ) returns value of false, where kc2 is the function object of m2.
	请按任意键继续. . .
	*/
}

//返回一个迭代到第一元件multimap中的,与一个键比指定的键等于或大于
void muiltmap_lower_bound(void)
{
	using namespace std;
	multimap <int, int> m1;
	multimap <int, int> ::const_iterator m1_AcIter, m1_RcIter;
	typedef pair <int, int> Int_Pair;

	m1.insert(Int_Pair(1, 10));
	m1.insert(Int_Pair(2, 20));
	m1.insert(Int_Pair(3, 20));
	m1.insert(Int_Pair(3, 30));

	m1_RcIter = m1.lower_bound(2);
	cout << "The element of multimap m1 with a key of 2 is: "
		<< m1_RcIter->second << "." << endl;

	m1_RcIter = m1.lower_bound(3);
	cout << "The first element of multimap m1 with a key of 3 is: "
		<< m1_RcIter->second << "." << endl;

	// If no match is found for the key, end( ) is returned
	m1_RcIter = m1.lower_bound(4);

	if (m1_RcIter == m1.end())
		cout << "The multimap m1 doesn't have an element "
		<< "with a key of 4." << endl;
	else
		cout << "The element of multimap m1 with a key of 4 is: "
		<< m1_RcIter->second << "." << endl;

	// The element at a specific location in the multimap can be
	// found using a dereferenced iterator addressing the location
	m1_AcIter = m1.end();
	m1_AcIter--;
	m1_RcIter = m1.lower_bound(m1_AcIter->first);
	cout << "The first element of m1 with a key matching\n"
		<< "that of the last element is: "
		<< m1_RcIter->second << "." << endl;

	// Note that the first element with a key equal to
	// the key of the last element is not the last element
	if (m1_RcIter == --m1.end())
		cout << "This is the last element of multimap m1."
		<< endl;
	else
		cout << "This is not the last element of multimap m1."
		<< endl;

	return;
	/*
	The element of multimap m1 with a key of 2 is: 20.
	The first element of multimap m1 with a key of 3 is: 20.
	The multimap m1 doesn't have an element with a key of 4.
	The first element of m1 with a key matching
	that of the last element is: 20.
	This is not the last element of multimap m1.
	请按任意键继续. . .
	*/
}

//返回multimap中的最大长度
void muiltmap_max_size(void)
{
	using namespace std;
	multimap <int, int> m1;
	multimap <int, int> ::size_type i;

	i = m1.max_size();
	cout << "The maximum possible length "
		<< "of the multimap is " << i << "." << endl;

	return;
	/*
	The maximum possible length of the multimap is 178956970.
	请按任意键继续. . .
	*/
}

//返回一个迭代解决的第一个元素在逆转多重映射
void muiltmap_rbegin(void)
{
	using namespace std;
	multimap <int, int> m1;

	multimap <int, int> ::iterator m1_Iter;
	multimap <int, int> ::reverse_iterator m1_rIter;
	multimap <int, int> ::const_reverse_iterator m1_crIter;
	typedef pair <int, int> Int_Pair;

	m1.insert(Int_Pair(1, 10));
	m1.insert(Int_Pair(2, 20));
	m1.insert(Int_Pair(3, 30));

	m1_rIter = m1.rbegin();
	cout << "The first element of the reversed multimap m1 is "
		<< m1_rIter->first << "." << endl;

	// begin can be used to start an iteration 
	// throught a multimap in a forward order
	cout << "The multimap is: ";
	for (m1_Iter = m1.begin(); m1_Iter != m1.end(); m1_Iter++)
		cout << m1_Iter->first << " ";
	cout << "." << endl;

	// rbegin can be used to start an iteration 
	// throught a multimap in a reverse order
	cout << "The reversed multimap is: ";
	for (m1_rIter = m1.rbegin(); m1_rIter != m1.rend(); m1_rIter++)
		cout << m1_rIter->first << " ";
	cout << "." << endl;

	// A multimap element can be erased by dereferencing its key 
	m1_rIter = m1.rbegin();
	m1.erase(m1_rIter->first);

	m1_rIter = m1.rbegin();
	cout << "After the erasure, the first element "
		<< "in the reversed multimap is "
		<< m1_rIter->first << "." << endl;

	return;
	/*
	The first element of the reversed multimap m1 is 3.
	The multimap is: 1 2 3 .
	The reversed multimap is: 3 2 1 .
	After the erasure, the first element in the reversed multimap is 2.
	请按任意键继续. . .
	*/
}

//返回解决了一个逆转多重映射成功的最后一个元素的位置的迭代器
void muiltmap_rend(void)
{
	using namespace std;
	multimap <int, int> m1;

	multimap <int, int> ::iterator m1_Iter;
	multimap <int, int> ::reverse_iterator m1_rIter;
	multimap <int, int> ::const_reverse_iterator m1_crIter;
	typedef pair <int, int> Int_Pair;

	m1.insert(Int_Pair(1, 10));
	m1.insert(Int_Pair(2, 20));
	m1.insert(Int_Pair(3, 30));

	m1_rIter = m1.rend();
	m1_rIter--;
	cout << "The last element of the reversed multimap m1 is "
		<< m1_rIter->first << "." << endl;

	// begin can be used to start an iteration 
	// throught a multimap in a forward order
	cout << "The multimap is: ";
	for (m1_Iter = m1.begin(); m1_Iter != m1.end(); m1_Iter++)
		cout << m1_Iter->first << " ";
	cout << "." << endl;

	// rbegin can be used to start an iteration 
	// throught a multimap in a reverse order
	cout << "The reversed multimap is: ";
	for (m1_rIter = m1.rbegin(); m1_rIter != m1.rend(); m1_rIter++)
		cout << m1_rIter->first << " ";
	cout << "." << endl;

	// A multimap element can be erased by dereferencing to its key 
	m1_rIter = --m1.rend();
	m1.erase(m1_rIter->first);

	m1_rIter = m1.rend();
	m1_rIter--;
	cout << "After the erasure, the last element "
		<< "in the reversed multimap is "
		<< m1_rIter->first << "." << endl;

	return;
	/*
	The last element of the reversed multimap m1 is 1.
	The multimap is: 1 2 3 .
	The reversed multimap is: 3 2 1 .
	After the erasure, the last element in the reversed multimap is 2.
	请按任意键继续. . .
	*/
}

//返回multimap中元素的数量
void muiltmap_size(void)
{
	using namespace std;
	multimap<int, int> m1, m2;
	multimap<int, int>::size_type i;
	typedef pair<int, int> Int_Pair;

	m1.insert(Int_Pair(1, 1));
	i = m1.size();
	cout << "The multimap length is " << i << "." << endl;

	m1.insert(Int_Pair(2, 4));
	i = m1.size();
	cout << "The multimap length is now " << i << "." << endl;

	return;
	/*
	The multimap length is 1.
	The multimap length is now 2.
	请按任意键继续. . .
	*/
}

//交换两个muiltmap的元素
void muiltmap_swap(void)
{
	using namespace std;
	multimap <int, int> m1, m2, m3;
	multimap <int, int>::iterator m1_Iter;
	typedef pair <int, int> Int_Pair;

	m1.insert(Int_Pair(1, 10));
	m1.insert(Int_Pair(2, 20));
	m1.insert(Int_Pair(3, 30));
	m2.insert(Int_Pair(10, 100));
	m2.insert(Int_Pair(20, 200));
	m3.insert(Int_Pair(30, 300));

	cout << "The original multimap m1 is:";
	for (m1_Iter = m1.begin(); m1_Iter != m1.end(); m1_Iter++)
		cout << " " << m1_Iter->second;
	cout << "." << endl;

	// This is the member function version of swap
	m1.swap(m2);

	cout << "After swapping with m2, multimap m1 is:";
	for (m1_Iter = m1.begin(); m1_Iter != m1.end(); m1_Iter++)
		cout << " " << m1_Iter->second;
	cout << "." << endl;

	// This is the specialized template version of swap
	swap(m1, m3);

	cout << "After swapping with m3, multimap m1 is:";
	for (m1_Iter = m1.begin(); m1_Iter != m1.end(); m1_Iter++)
		cout << " " << m1_Iter->second;
	cout << "." << endl;

	return;
	/*
	The original multimap m1 is: 10 20 30.
	After swapping with m2, multimap m1 is: 100 200.
	After swapping with m3, multimap m1 is: 300.
	请按任意键继续. . .
	*/
}

//返回一个迭代器的第一个元素multimap中以关键比指定的键更大
void muiltmap_upper_bound(void)
{
	using namespace std;
	multimap <int, int> m1;
	multimap <int, int> ::const_iterator m1_AcIter, m1_RcIter;
	typedef pair <int, int> Int_Pair;

	m1.insert(Int_Pair(1, 10));
	m1.insert(Int_Pair(2, 20));
	m1.insert(Int_Pair(3, 30));
	m1.insert(Int_Pair(3, 40));

	m1_RcIter = m1.upper_bound(1);
	cout << "The 1st element of multimap m1 with "
		<< "a key greater than 1 is: "
		<< m1_RcIter->second << "." << endl;

	m1_RcIter = m1.upper_bound(2);
	cout << "The first element of multimap m1 with a key "
		<< " greater than 2 is: "
		<< m1_RcIter->second << "." << endl;

	// If no match is found for the key, end( ) is returned
	m1_RcIter = m1.lower_bound(4);

	if (m1_RcIter == m1.end())
		cout << "The multimap m1 doesn't have an element "
		<< "with a key of 4." << endl;
	else
		cout << "The element of multimap m1 with a key of 4 is: "
		<< m1_RcIter->second << "." << endl;

	// The element at a specific location in the multimap can be
	// found using a derefenced iterator addressing the location
	m1_AcIter = m1.begin();
	m1_RcIter = m1.upper_bound(m1_AcIter->first);
	cout << "The first element of m1 with a key greater than\n"
		<< "that of the initial element of m1 is: "
		<< m1_RcIter->second << "." << endl;

	return;
	/*
	The 1st element of multimap m1 with a key greater than 1 is: 20.
	The first element of multimap m1 with a key  greater than 2 is: 30.
	The multimap m1 doesn't have an element with a key of 4.
	The first element of m1 with a key greater than
	that of the initial element of m1 is: 20.
	请按任意键继续. . .
	*/
}

//成员函数返回一个功能对象,通过比较它们的键值确定multimap中的元素的顺序
void muiltmap_value_comp(void)
{
	using namespace std;

	multimap <int, int, less<int> > m1;
	multimap <int, int, less<int> >::value_compare vc1 = m1.value_comp();
	multimap<int, int>::iterator Iter1, Iter2;

	Iter1 = m1.insert(multimap <int, int> ::value_type(1, 10));
	Iter2 = m1.insert(multimap <int, int> ::value_type(2, 5));

	if (vc1(*Iter1, *Iter2) == true)
	{
		cout << "The element ( 1,10 ) precedes the element ( 2,5 )."
			<< endl;
	}
	else
	{
		cout << "The element ( 1,10 ) does "
			<< "not precede the element ( 2,5 )."
			<< endl;
	}

	if (vc1(*Iter2, *Iter1) == true)
	{
		cout << "The element ( 2,5 ) precedes the element ( 1,10 )."
			<< endl;
	}
	else
	{
		cout << "The element ( 2,5 ) does "
			<< "not precede the element ( 1,10 )."
			<< endl;
	}

	return;
	/*
	The element ( 1,10 ) precedes the element ( 2,5 ).
	The element ( 2,5 ) does not precede the element ( 1,10 ).
	请按任意键继续. . .
	*/
}


 
 


你可能感兴趣的:(c++ Muitmaps)