Multiset

作者在 2008-08-17 14:28:27 发布以下内容

//multiset

#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
int main(void)
{
   
multiset<int> mul1;       //default constructor
   
int arr[] = {0,0,1,1,2,3,4,5};
    multiset<int> mul2(arr,arr+8);
    multiset<int> mul3(mul2);
    multiset<int>::iterator Iter;
    multiset<int>::reverse_iterator RevIter;
    if (mul1.empty()) cout<<"mul1 is empty"<<endl;
    else cout<<"mul1 is not empty"<<endl;
    cout<<"mul2(using begin,end)is ";
    for (Iter = mul2.begin();Iter != mul2.end();Iter++) cout<<*Iter<<" ";
    cout<<endl;
    cout<<"mul2(using rbegin,rend)is ";
    for (RevIter = mul2.rbegin();RevIter != mul2.rend();RevIter++) cout<<*RevIter<<" ";
    cout<<endl;
    mul1.insert(arr,arr+8);
    mul1.insert(5);
    //采用count进行元素出现统计
   
cout<<"Number elements in mul1 that match 1 is "<<mul1.count(1)<<endl;
    multiset<int>::const_iterator constIter = mul1.find(3);
    if (constIter != mul1.end()) cout<<"mul1 contains element 3,*constIter = "<<*constIter<<endl;
    cout<<"maxSize/size"<<mul1.max_size()<<"/"<<mul1.size()<<endl;
    mul1.insert(4);
    mul2.swap(mul1);
    cout<<"the last element of nul2 is "<<*mul2.rbegin()<<endl;
    mul1.clear();
    cout<<"After calling mul1.clear(),mul1.size() is "<<mul1.size()<<endl;
    //采用get_allocator()返回在内存分配器
   
multiset<int>::allocator_type Alloc1 = mul1.get_allocator();
    //采用key_compare进行键值比较
   
multiset<int>::key_compare kc = mul2.key_comp();
    bool result = kc(2,3);
    if (result ==true) cout<<"kc is a function object using by mul2.kc(2,3) = true"<<endl;
    else cout<<"kc is a function object using by mul2.kc(2,3) =false"<<endl;
    //采用value_comp进行实值比较
   
multiset<int>::value_compare vc = mul2.value_comp();
    result = vc(10,4);
    if (result == true) cout<<"vc is a function object using by mul2.vc(10,4) = true"<<endl;
    else cout<<"vc is a function object using by mul2.vc(10,4) = false"<<endl;
    //采用upper_bound返回上迭代器
   
cout<<"*(mul2.upper_bound(3) = "<<*mul2.upper_bound(3)<<endl;
    cout<<"*(mul2.lower_bound(3) = "<<*mul2.lower_bound(3)<<endl;
    //采用equal_range返回当前元素的上下迭代器
   
pair<multiset<int>::const_iterator,multiset<int>::const_iterator> pr1 = mul2.equal_range(3);
    cout<<"*pr1,first = "<<*pr1.first<<"\t"<<"*pr1.second = "<<*pr1.second<<endl;
    if (mul3.erase(1) != 0) cout<<"mul3 does not contain 1 any more"<<endl;
    else cout<<"No elements in mul3 match key 1 "<<endl;
    //if ((mul2.erase(mul2.begin())) != mul2.end()) cout<<"mul2 does not contain 0 any more"<<endl;
    //else cout<<"No elements in mul2 match key 0"<<endl;
   
mul3.erase(mul3.begin(),mul3.end());
    cout<<"After mul3.erase(mul3.begin(),mul3.end()),mul3.size() = "<<mul3.size()<<endl;
    return 0;
}

STL | 阅读 4124 次
文章评论,共0条
游客请输入验证码
浏览56289次
最新评论