CHAPTER9-40.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:4k
- //文件名:CHAPTER9-40.cpp
- #pragma warning(disable: 4786)
- #include <set>
- #include <iostream>
- int main()
- {
- //采用默认构造器构造c1
- std::multiset<int> c1 ;
- int ai[] = {0, 0, 1, 1, 2, 2, 3, 4} ;
- //创建multset变量c2,并且使用迭代器初始化其范围
- std::multiset<int> c2(ai, ai + 8) ;
- //采用拷贝构造函数方式
- std::multiset<int> c3(c2) ;
- std::multiset<int>::iterator Iter ;
- std::multiset<int>::reverse_iterator RevIter ;
- //采用empty判断multiset变量c1是否为空
- if(c1.empty())
- { std::cout << "multiset c1 is empty" << std::endl ; }
- else
- { std::cout << "multiset c1 is not empty" << std::endl ; }
- //采用begin, end进行访问头和尾访问
- std::cout << "c2 (using begin, end) = " ;
- for(Iter = c2.begin(); Iter != c2.end(); Iter++)
- { std::cout << *Iter << " " ; }
- std::cout << std::endl ;
- //采用rbegin, rend进行访问头和尾访问
- std::cout << "c2 (using rbegin, rend) = " ;
- for(RevIter = c2.rbegin(); RevIter != c2.rend(); RevIter++)
- { std::cout << *RevIter << " " ; }
- std::cout << std::endl ;
- //采用函数insert插入c1元素
- c1.insert(ai, ai+4) ;
- c1.insert(1) ;
- //采用count进行元素出现统计
- std::cout << "Number elements in c1 that match 1 = " << c1.count(1) << std::endl ;
- //采用find进行元素查找
- std::multiset<int>::const_iterator constIter = c1.find(3) ;
- if(constIter != c1.end())
- {std::cout << "c1 contains element 3, *constIter = " << *constIter << std::endl ;}
- //采用max_size返回c1最大元素数量
- std::cout << "c1.max_size() = " << c1.max_size() << std::endl ;
- //采用size返回c1元素数量
- std::cout << "c1.size() = " << c1.size() << std::endl ;
- //采用swap进行c1和c2的元素交换
- c1.insert(4) ;
- c2.swap(c1) ;
- std::cout << "The last element of c2 = " << *(c2.rbegin())<< std::endl ;
- //采用clear进行c1对象清除
- c1.clear() ;
- std::cout << "After calling c1.clear(), c1.size() = " << c1.size() << std::endl ;
- //采用get_allocator返回内存分配器
- std::multiset<int>::allocator_type a1 = c1.get_allocator() ;
- //采用key_compare进行键值比较
- std::multiset<int>::key_compare kc = c2.key_comp() ;
- bool result = kc(2, 3) ;
- if(result == true)
- {std::cout << "kc is function object used by c2. kc(2,3) = true" << std::endl ; }
- else
- {std::cout << "kc is function object used by c2. kc(2,3) = false" << std::endl ; }
- //采用value_comp进行实值比较
- std::multiset<int>::value_compare vc = c2.value_comp() ;
- result = vc(10, 4) ;
- if(result == true)
- {std::cout << "vc is function object used by c2. vc(10,4) = true" << std::endl ; }
- else
- { std::cout << "vc is function object used by c2. vc(10,4) = false" << std::endl ; }
- //采用upper_bound返回上迭代器
- std::cout << "* (c2.upper_bound(3)) = " << *(c2.upper_bound(3)) << std::endl ;
- //采用lower_bound返回低迭代器
- std::cout << "* (c2.lower_bound(3)) = "<< *(c2.lower_bound(3)) << std::endl ;
- //采用equal_range返回当前元素的上下迭代器
- std::pair<std::multiset<int>::const_iterator, std::multiset<int>::const_iterator> pr1 = c2.equal_range(3) ;
- std::cout << "*(pr1.first) = " << *(pr1.first) << "t"
- << "*(pr1.second) = " << *(pr1.second) << std::endl ;
- //采用erase进行元素删除
- if(c3.erase(1) != 0)
- { std::cout << "c3 does not contain 1 any more" << std::endl ; }
- else
- { std::cout << "No elements in c3 match key 1" << std::endl ; }
- if((c2.erase(c2.begin())) != c2.end())
- { std::cout << "c2 does not contain 0 any more" << std::endl ; }
- else
- { std::cout << "No elements in c2 match key 0" << std::endl ; }
- c3.erase(c3.begin(), c3.end()) ;
- std::cout << "after c3.erase(c3.begin(), c3.end()), c3.size() = "
- << c3.size() << std::endl ;
- return 0 ;
- }