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

📄 chapter5-21.cpp

📁 STL程序员开发指南源码
💻 CPP
字号:
//文件名:CHAPTER5-21.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()
{//default constructor
	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"))};
	//construct from a range
//	MAP_INT_STR c2(pairs, pairs + 5) ;
	MAP_INT_STR c2;
	c2.insert(MAP_INT_STR::value_type(1, std::string("one")));
	c2.insert(MAP_INT_STR::value_type(2, std::string("two")));
	c2.insert(MAP_INT_STR::value_type(3, std::string("three")));
	c2.insert(MAP_INT_STR::value_type(4, std::string("four")));
	c2.insert(MAP_INT_STR::value_type(5, std::string("five")));
	//copy constructor
	MAP_INT_STR c3(c2) ;
	//empty
	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(MAP_INT_STR::value_type(1, std::string("one")));
	c1.insert(MAP_INT_STR::value_type(2, std::string("two")));
	c1.insert(MAP_INT_STR::value_type(3, std::string("three")));
	c1.insert(MAP_INT_STR::value_type(4, std::string("four")));
	c1.insert(MAP_INT_STR::value_type(5, std::string("five")));

	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 + -