⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter10-41.cpp

📁 C++STL程序员开发指南
💻 CPP
字号:
//文件名:CHAPTER10-41.cpp
#pragma warning(disable: 4786)
#include <map>
#include <iostream>
#include <string>
typedef std::map<int, std::string> MAP_INT_STR ;
typedef MAP_INT_STR::iterator MAP_ITERATOR ;
typedef MAP_INT_STR::reverse_iterator MAP_REVERSE_ITERATOR ;
typedef std::pair<int, std::string> PAIR_INT_STR ;
template <class ITERATOR>
void print_map_item(ITERATOR it)
{ 	std::cout << (*it).first << ", " <<	(*it).second << std::endl ;}
int main()
{
	//默认构造函数
	MAP_INT_STR c1 ;	
	PAIR_INT_STR pairs[5] = {	PAIR_INT_STR(1, std::string("one")),
								PAIR_INT_STR(2, std::string("two")),
								PAIR_INT_STR(3, std::string("three")),
								PAIR_INT_STR(4, std::string("four")),
								PAIR_INT_STR(5, std::string("five"))
							};
	//定义map变量,并且通过数组赋值
	MAP_INT_STR c2(pairs, pairs + 5) ;
	//拷贝构造函数
	MAP_INT_STR c3(c2) ;
	//判断是否为空值
	if(c1.empty())
	{		std::cout << "c1 is empty" << std::endl ;	}
	else
	{		std::cout << "c1 is not empty" << std::endl ;	}
	//采用begin, end获取首末迭代器
	std::cout << "c2 (using begin, end) = " << std::endl ;
	MAP_ITERATOR Iter1 ;
	for(Iter1 = c2.begin(); Iter1 != c2.end(); Iter1++)
	{		print_map_item(Iter1) ;	}
	//采用rbegin, rend获取反向首末迭代器
	std::cout << "c2 (using rbegin, rend) = " << std::endl ;
	MAP_REVERSE_ITERATOR RevIter1 ;
	for(RevIter1 = c2.rbegin(); RevIter1 != c2.rend(); RevIter1++)
	{		print_map_item(RevIter1) ;	}
	//采用insert插入元素
	std::pair<MAP_ITERATOR, bool> result ;
	result = c1.insert(MAP_INT_STR::value_type(6, std::string("six"))) ;
	if(result.second == true)
	{	std::cout << "a pair of key/data was inserted in c1, *(result.first) = " ;		print_map_item(result.first); 
	}
	else
	{		std::cout << "pair(6, \"six\") was not inserted in c1" << std::endl ;	}
	c1.insert(pairs, pairs + 5) ;
	c1.insert(c1.begin(), PAIR_INT_STR(0, std::string("zero"))) ;
	//采用find进行元素查找
	std::cout << "Does c1 contain any pair with key = 6?" << std::endl ;
	Iter1 = c1.find(6) ;
	if(Iter1 != c1.end())
	{
		std::cout << "c1 contains pair:" ;
		print_map_item(Iter1) ;
	}
	else
	{		std::cout << "c1 does not contain any element with key = 6" << std::endl ;	}
	//采用operator[]进行元素赋值
	c1[8] = "eight" ;
	std::cout << "Last key/data pair in c1 = " ;
	print_map_item(c1.rbegin()) ;
	//利用max_size获取最大元素大小
	std::cout << "max elements which c1 can hold uisng current allocator = "
		<< c1.max_size() << std::endl ;
	//利用size获取元素实际大小
	std::cout << "number of elements in c1 = " << c1.size() << std::endl ;
	//采用swap进行元素交换
	c1.swap(c2) ;
	std::cout << "Last key/data pair in c1 = " ;
	print_map_item(c1.rbegin()) ;
	//采用clear进行元素清除
	c3.clear() ;
	std::cout << "after calling c3.clear(), number of elements in c3 = " << c3.size() << std::endl ;
	//get_allocator获取内存分配器
	MAP_INT_STR::allocator_type a1 = c3.get_allocator() ;
	//key_comp比较关键值
	MAP_INT_STR::key_compare kc = c1.key_comp() ;
	std::cout << "use function object kc to find less of (10, 4)..." << std::endl ;
	if (kc(10, 4) == true)
		std::cout << "kc(10, 4) == true, which means 10 < 4" << std::endl ;
	else
		std::cout << "kc(10, 4) == false, which means 10 > 4" << std::endl ;
	//value_comp比较实值
	MAP_INT_STR::value_compare vc = c1.value_comp() ;
	std::cout << "use function object vc to compare int-string pairs..." << std::endl ;
	std::cout << "pairs[0] = (" << pairs[0].first << ", " << pairs[0].second << ")" << std::endl ;
	std::cout << "pairs[1] = (" << pairs[1].first << ", " 
		<< pairs[1].second << ")" << std::endl ;
	if ( vc(pairs[0], pairs[1]) == true)
		std::cout << "pairs[0] < pairs[1]" << std::endl ;
	else
		std::cout << "pairs[0] > pairs[1]" << std::endl ;
	//获取上迭代器upper_bound
	Iter1 = c2.upper_bound(6) ;
	std::cout << "first map element with key > 6 = " ;
	print_map_item(Iter1) ;
	//获取下迭代器lower_bound
	Iter1 = c2.lower_bound(6) ;
	std::cout << "first map element with key 6 = " ;
	print_map_item(Iter1) ;
	//获取上下迭代器equal_range
	std::pair<MAP_ITERATOR, MAP_ITERATOR> pair2 = c2.equal_range(6) ;
	std::cout << "using c2.equal_range(6),first map element with key > 6 = " ;
	print_map_item(pair2.second) ;
	std::cout << "using c2.equal_range(6), first map element with key = 6 = " ;
	print_map_item(pair2.first) ;
//采用count进行元素统计
	std::cout << "does c2 contain an element with key 8 ?" << std::endl ;
	if(c2.count(8) == 1)
	{	std::cout << "c2 contains element with key 8" << std::endl ;	}
	else
	{		std::cout << "c2 does not contain element with key 8" << std::endl ;	}
	//采用erase删除元素
	c2.erase(c2.begin()) ;
	std::cout << "first key/data pair of c2 is: " ;
	print_map_item(c2.begin()) ;
	c1.erase(c1.begin(), c1.end()) ;
	std::cout << "after c1.erase(c1.begin(), c2.end()), number of elements in c1 = "
		<< c1.size() << std::endl ;
	if(c2.erase(8) == 1)
	{		std::cout << "element with key 8 in c2 was erased" << std::endl ;	}
	else
	{		std::cout << "c2 does not contain any element with key 8" << std::endl ;	}	
	return 0 ;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -