c++ maps

map是存储由密钥值和映射值的组合构成的元素,下列特定的顺序关联容器。
在map中,密钥值一般用于排序和唯一标识的元素,而映射的值存储关联于该键的内容。类型的键和映射值可能不同,并且在构件的类型的value_type,这是一对型组合都被分组在一起:
ms-help://MS.MSDNQTR.v90.chs/dv_vcstdlib/html/d590e984-54b0-43cf-a72a-90d70171ac3e.htm
http://www.cplusplus.com/reference/map/map/
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	获得分配



#include <map>
#include <iostream>
#include <xfunctional>

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

//返回指向map中的第一个元素的迭代器
void map_begin(void);

//擦除的map的所有元素
void map_clear(void);

//返回一个map,其中关键的参数中指定的关键字相匹配元件的数目
void map_count(void);

//判断是否为空
void map_empty(void);

//返回一个指向map的最后一个元素之后位置的迭代器。
void map_end(void);

//返回一对迭代器。在一对点的第一个迭代的第一元件与一个键比指定的键更大的map。在一对指向一个密钥等于或大于此键映射中的第一元件的第二迭代。
void map_equal_range(void);

//删除元素或一个范围从指定的位置在map的元素。
void map_erase(void);

//返回指向一个元素的位置在map上,有一个键等于指定密钥的迭代器。
void map_find(void);

//返回一个用于构造图分配器对象的副本。
void map_get_allocator(void);

//在插入指定位置的元素或一系列元素融入map
void map_insert(void);

//返回用于在map命令密钥的比较对象的副本
void map_key_comp(void);

//返回一个迭代的第一元件中的map具有密钥值,该值大于指定的键的等于或大于
void map_lower_bound(void);

//返回map的最大长度
void map_max_size(void);

//返回指向第一元件以相反的map的迭代
void map_rbegin(void);

//返回一个指向一个反向映射的最后一个元素之后位置的迭代器
void map_rend(void);

//返回该map要素的数量
void map_size(void);

//两个map的交换元素
void map_swap(void);

//返回一个迭代器的第一个元素在map中有一个关键值比指定的键更大
void map_upper_bound(void);

//检索比较对象的副本,用于在map中订购元素值
void map_value_comp(void);


int main()
{
	//mapConstructor();
	//map_begin();
	//map_clear();
	//map_count();
	//map_empty();
	//map_end();
	//map_equal_range();
	//map_erase();
	//map_find();
	//map_get_allocator();
	//map_insert();
	//map_key_comp();
	//map_lower_bound();
	//map_max_size();
	//map_rbegin();
	//map_rend();
	//map_size();
	//map_swap();
	//map_upper_bound();
	map_value_comp();
	return 0;
}

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

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

	// Create an empty map m1 with the key comparison
	// function of less than, then insert 4 elements
	map <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 map m2 with the key comparison
	// function of geater than, then insert 2 elements
	map <int, int, greater<int> > m2;
	m2.insert(Int_Pair(1, 10));
	m2.insert(Int_Pair(2, 20));

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

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

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

	// Create a map m6 by copying the range m4[_First, _Last)
	// and with the allocator of map m2
	map <int, int>::allocator_type m2_Alloc;
	m2_Alloc = m2.get_allocator();
	map <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
	请按任意键继续. . .
	*/
}

//返回指向map中的第一个元素的迭代器
void map_begin(void)
{
	using namespace std;
	map <int, int> m1;

	map <int, int> ::iterator m1_Iter;
	map <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 because the iterator is const
	// m1_cIter = m1.begin ( );
	// m1.erase ( m1_cIter );

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

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

//擦除的map的所有元素
void map_clear(void)
{
	using namespace std;
	map<int, int> m1;
	map<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 map is initially "
		<< i << "." << endl;

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

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

//返回一个map,其中关键的参数中指定的关键字相匹配元件的数目
void map_count(void)
{
	using namespace std;
	map<int, int> m1;
	map<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));

	// Keys must be unique in map, so duplicates are ignored
	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: 1.
	The number of elements in m1 with a sort key of 2 is: 1.
	The number of elements in m1 with a sort key of 3 is: 0.
	请按任意键继续. . .
	*/
}

//判断是否为空
void map_empty(void)
{
	using namespace std;
	map <int, int> m1, m2;

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

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

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

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

//返回一个指向map的最后一个元素之后位置的迭代器。
void map_end(void)
{
	using namespace std;
	map <int, int> m1;

	map <int, int> ::iterator m1_Iter;
	map <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 the last element of m1 is:\n"
		<< 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 the last element of m1 is now:\n"
		<< m1_cIter->second << endl;

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

	*/
}

void map_equal_range(void)
{
	using namespace std;
	typedef map <int, int, less<int> > IntMap;
	IntMap m1;
	map <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 <IntMap::const_iterator, IntMap::const_iterator> p1, p2;
	p1 = m1.equal_range(2);

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

	cout << "The upper bound of the element with "
		<< "a key of 2 in the map 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 map m1 doesn't have an element "
		<< "with a key less than 40." << endl;
	else
		cout << "The element of map m1 with a key >= 40 is: "
		<< p2.first->first << "." << endl;

	return;
	/*
	The lower bound of the element with a key of 2 in the map m1 is: 20.
	The upper bound of the element with a key of 2 in the map 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 map m1 doesn't have an element with a key less than 40.
	请按任意键继续. . .
	*/
}

//删除元素或一个范围从指定的位置在map的元素。
void map_erase(void)
{
	using namespace std;
	map<int, int> m1, m2, m3;
	map<int, int>::iterator pIter, Iter1, Iter2;
	int i;
	map<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 map m1 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 map 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
	n = m3.erase(2);

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

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

//返回指向一个元素的位置在map上,有一个键等于指定密钥的迭代器。
void map_find(void)
{
	using namespace std;
	map <int, int> m1;
	map <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_RcIter = m1.find(2);
	cout << "The element of map m1 with a key of 2 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 map m1 doesn't have an element "
		<< "with a key of 4." << endl;
	else
		cout << "The element of map m1 with a key of 4 is: "
		<< m1_RcIter->second << "." << endl;

	// The element at a specific location in the map 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 element of m1 with a key matching "
		<< "that of the last element is: "
		<< m1_RcIter->second << "." << endl;

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

//返回一个用于构造图分配器对象的副本。
void map_get_allocator(void)
{
	using namespace std;
	map <int, int>::allocator_type m1_Alloc;
	map <int, int>::allocator_type m2_Alloc;
	map <int, double>::allocator_type m3_Alloc;
	map <int, int>::allocator_type m4_Alloc;

	// The following lines declare objects
	// that use the default allocator.
	map <int, int> m1;
	map <int, int, allocator<int> > m2;
	map <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\n"
		<< "before free memory is exhausted: "
		<< m2.max_size() << ".\n" << endl;

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

	// The following line creates a map m4
	// with the allocator of map 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 with 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.
	请按任意键继续. . .
	*/
}

//在插入指定位置的元素或一系列元素融入map
void map_insert(void)
{
	using namespace std;
	map <int, int>::iterator m1_pIter, m2_pIter;

	map <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));
	m1.insert(Int_Pair(4, 40));

	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;

	pair< map<int, int>::iterator, bool > pr;
	pr = m1.insert(Int_Pair(1, 10));

	if (pr.second == true) {
		cout << "The element 10 was inserted in m1 successfully." << endl;
	}
	else {
		cout << "Key number 1 already exists in m1\n"
			<< "with an associated value of ( (pr.first) -> second ) = "
			<< (pr.first)->second
			<< "." << endl;
	}

	// The hint version of insert
	m1.insert(--m1.end(), Int_Pair(5, 50));

	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;
}

//返回用于在map命令密钥的比较对象的副本
void map_key_comp(void)
{
	using namespace std;

	map <int, int, less<int> > m1;
	map <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;
	}

	map <int, int, greater<int> > m2;
	map <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.
	请按任意键继续. . .
	*/
}

//返回一个迭代的第一元件中的map具有密钥值,该值大于指定的键的等于或大于
void map_lower_bound(void)
{
	using namespace std;
	map <int, int> m1;
	map <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_RcIter = m1.lower_bound(2);
	cout << "The first element of map m1 with a key of 2 is: "
		<< m1_RcIter->second << "." << endl;

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

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

	// The element at a specific location in the map 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 element of m1 with a key matching "
		<< "that of the last element is: "
		<< m1_RcIter->second << "." << endl;

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

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

	i = m1.max_size();
	cout << "The maximum possible length "
		<< "of the map is " << i << "."
		<< endl << "(Magnitude is machine specific.)";

	return;
	/*
	The maximum possible length of the map is 178956970.
	(Magnitude is machine specific.)请按任意键继续. . .
	*/
}

//返回指向第一元件以相反的map的迭代
void map_rbegin(void)
{
	using namespace std;
	map <int, int> m1;

	map <int, int> ::iterator m1_Iter;
	map <int, int> ::reverse_iterator m1_rIter;
	map <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 map m1 is "
		<< m1_rIter->first << "." << endl;

	// begin can be used to start an iteration 
	// through a map in a forward order
	cout << "The map 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 
	// through a map in a reverse order
	cout << "The reversed map is: ";
	for (m1_rIter = m1.rbegin(); m1_rIter != m1.rend(); m1_rIter++)
		cout << m1_rIter->first << " ";
	cout << "." << endl;

	// A map element can be erased by dereferencing to 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 map is "
		<< m1_rIter->first << "." << endl;

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

//返回一个指向一个反向映射的最后一个元素之后位置的迭代器
void map_rend(void)
{
	using namespace std;
	map <int, int> m1;

	map <int, int> ::iterator m1_Iter;
	map <int, int> ::reverse_iterator m1_rIter;
	map <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 map m1 is "
		<< m1_rIter->first << "." << endl;

	// begin can be used to start an iteration 
	// through a map in a forward order
	cout << "The map 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 
	// through a map in a reverse order
	cout << "The reversed map is: ";
	for (m1_rIter = m1.rbegin(); m1_rIter != m1.rend(); m1_rIter++)
		cout << m1_rIter->first << " ";
	cout << "." << endl;

	// A map 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 map is "
		<< m1_rIter->first << "." << endl;


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

//返回该map要素的数量
void map_size(void)
{
	using namespace std;
	map<int, int> m1, m2;
	map<int, int>::size_type i;
	typedef pair<int, int> Int_Pair;

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

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

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

//两个map的交换元素
void map_swap(void)
{

	using namespace std;
	map <int, int> m1, m2, m3;
	map <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 map 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
	//m2 is said to be the argument map; m1 the target map
	m1.swap(m2);

	cout << "After swapping with m2, map 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, map m1 is:";
	for (m1_Iter = m1.begin(); m1_Iter != m1.end(); m1_Iter++)
		cout << " " << m1_Iter->second;
	cout << "." << endl;

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

//返回一个迭代器的第一个元素在map中有一个关键值比指定的键更大
void map_upper_bound(void)
{
	using namespace std;
	map <int, int> m1;
	map <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_RcIter = m1.upper_bound(2);
	cout << "The first element of map 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.upper_bound(4);

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

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

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

//检索比较对象的副本,用于在map中订购元素值
void map_value_comp(void)
{
	using namespace std;

	map <int, int, less<int> > m1;
	map <int, int, less<int> >::value_compare vc1 = m1.value_comp();
	pair< map<int, int>::iterator, bool > pr1, pr2;

	pr1 = m1.insert(map <int, int> ::value_type(1, 10));
	pr2 = m1.insert(map <int, int> ::value_type(2, 5));

	if (vc1(*pr1.first, *pr2.first) == 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(*pr2.first, *pr1.first) == 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++ maps)