CHAPTER10-42.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:6k
- //文件名:CHAPTER10-42.cpp
- #pragma warning(disable: 4786)
- #include <map>
- #include <iostream>
- #include <string>
- typedef std::multimap<char, std::string> multimap_INT_STR ;
- typedef multimap_INT_STR::iterator multimap_ITERATOR ;
- typedef multimap_INT_STR::reverse_iterator multimap_REVERSE_ITERATOR ;
- typedef std::pair<char, std::string> PAIR_INT_STR ;
- template <class ITERATOR>
- void print_multimap_item(ITERATOR it)
- { std::cout << (*it).first << ", " << (*it).second << std::endl ;}
- int main()
- {
- //默认构造函数定义变量c1
- multimap_INT_STR c1 ;
- PAIR_INT_STR pairs[5] = { PAIR_INT_STR('a', std::string("ATL")),
- PAIR_INT_STR('a', std::string("ADO")),
- PAIR_INT_STR('b', std::string("BASIC")),
- PAIR_INT_STR('c', std::string("COM")),
- PAIR_INT_STR('d', std::string("DAO"))
- };
- //定义变量c2,并且其值通过数组进行初始化
- //multimap_INT_STR c2(pairs, pairs + 5) ;
- multimap_INT_STR c2 ;
- c2.insert(multimap_INT_STR::value_type('a', std::string("ATL"))) ;
- c2.insert(multimap_INT_STR::value_type('a', std::string("ADO"))) ;
- c2.insert(multimap_INT_STR::value_type('b', std::string("BASIC"))) ;
- c2.insert(multimap_INT_STR::value_type('c', std::string("COM"))) ;
- c2.insert(multimap_INT_STR::value_type('d', std::string("DAO"))) ;
- //拷贝型构造函数
- multimap_INT_STR c3(c2) ;
- //利用empty判断c1是否为空
- 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 ;
- multimap_ITERATOR Iter1 ;
- for(Iter1 = c2.begin(); Iter1 != c2.end(); Iter1++)
- { print_multimap_item(Iter1) ; }
- //利用rbegin, rend返回反向首末迭代器
- std::cout << "c2 (using rbegin, rend) = " << std::endl ;
- multimap_REVERSE_ITERATOR RevIter1 ;
- for(RevIter1 = c2.rbegin(); RevIter1 != c2.rend(); RevIter1++)
- { print_multimap_item(RevIter1) ; }
- //利用insert插入元素
- Iter1 = c1.insert(multimap_INT_STR::value_type('i', std::string("Internet"))) ;
- if(Iter1 != c1.end())
- { std::cout << "a pair of key/data was inserted in c1, *Iter1 = " ;
- print_multimap_item(Iter1);
- }
- else
- { std::cout << "pair('i', "Internet") was not inserted in c1" << std::endl ; }
- //c1.insert(pairs, pairs + 5) ;
- c1.insert(multimap_INT_STR::value_type('a', std::string("ATL"))) ;
- c1.insert(multimap_INT_STR::value_type('a', std::string("ADO"))) ;
- c1.insert(multimap_INT_STR::value_type('b', std::string("BASIC"))) ;
- c1.insert(multimap_INT_STR::value_type('c', std::string("COM"))) ;
- c1.insert(multimap_INT_STR::value_type('d', std::string("DAO"))) ;
- c1.insert(c1.begin(), PAIR_INT_STR('j', std::string("java"))) ;
- //利用find查找元素
- std::cout << "Does c1 contain any pair with key = j?" << std::endl ;
- Iter1 = c1.find('j') ;
- if(Iter1 != c1.end())
- {
- std::cout << "c1 contains pair:" ;
- print_multimap_item(Iter1) ;
- }
- else
- { std::cout << "c1 does not contain any element with key = j" << std::endl ; }
- //根据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_multimap_item(c1.rbegin()) ;
- //使用clear进行元素清除
- c3.clear() ;
- std::cout << "after calling c3.clear(), number of elements in c3 = " << c3.size() << std::endl ;
- //根据get_allocator获取内存分配器
- multimap_INT_STR::allocator_type a1 = c3.get_allocator() ;
- //key_comp进行键值比较
- multimap_INT_STR::key_compare kc = c1.key_comp() ;
- std::cout << "use function object kc to find less of ('a', 'b')..." << std::endl ;
- if (kc('a', 'b') == true)
- std::cout << "kc('a', 'b') == true, which means 'a' < 'b'" << std::endl ;
- else
- std::cout << "kc('a', 'b') == false, which means 'a' > 'b'" << std::endl ;
- //value_comp进行实值比较
- multimap_INT_STR::value_compare vc = c1.value_comp() ;
- std::cout << "use function object vc to compare char-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('c') ;
- std::cout << "first multimap element with key > 'c' = " ;
- print_multimap_item(Iter1) ;
- //lower_bound返回下迭代器
- Iter1 = c2.lower_bound('c') ;
- std::cout << "first multimap element with key 'c' = " ;
- print_multimap_item(Iter1) ;
- //equal_range返回上下迭代器
- std::pair<multimap_ITERATOR, multimap_ITERATOR> pair2 = c2.equal_range('c') ;
- std::cout << "using c2.equal_range('c'),first multimap element with key > 'c' = " ;
- print_multimap_item(pair2.second) ;
- std::cout << "using c2.equal_range('c'), first multimap element with key = 'c' = " ;
- print_multimap_item(pair2.first) ;
- //count进行元素统计
- std::cout << "number of pairs in c2 with key 'a' = " << c2.count('a') << std::endl ;
- //erase进行所有元素清除
- c2.erase(c2.begin()) ;
- std::cout << "first key/data pair of c2 is: " ;
- print_multimap_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('j') == 1)
- { std::cout << "element with key 'j' in c2 was erased" << std::endl ; }
- else
- { std::cout << "c2 does not contain any element with key 'j'" << std::endl ; }
- return 0 ;
- }