C++ multiset容器元素的插入搜索遍历

5 篇文章 0 订阅
int main(int argc, const char *argv[])
{
    multiset<string> mset_str; 
    
    string str;
    cout<<"insert element to the multiset"<<endl;
    while(cin>>str)
        mset_str.insert(str);
    cin.clear();

    cout<<"the multiset element are: "<<endl;
    for(multiset<string>::iterator iter=mset_str.begin();
            iter!=mset_str.end();
            ++iter)
    {
        cout<<*iter<<endl; 
    }	
        
    cout<<"now please which element you want to search?"<<endl;
    string search_str;
    
    while(cin>>search_str)
    {
        typedef multiset<string>::size_type ms_type;
        typedef multiset<string>::iterator ms_iter; 
        
        ms_type cnts=mset_str.count(search_str);
        ms_iter miter=mset_str.find(search_str);
        
        for(ms_type cnt=0;cnt!=cnts;cnt++,++miter)
		{
            cout<<*miter<<" "<<cnt<<endl;
		}
    }
    
    system("pause");
    return 0;
}

insert element to the multiset
111 222 333 444 555 666
abc def hij klm
111 222 333 444 555 666
abc def hij klm
111 222 333 444 555 666
abc def hij klm
111 222 333 444 555 666
abc def hij klm
^Z
the multiset element are:
111
111
111
111
222
222
222
222
333
333
333
333
444
444
444
444
555
555
555
555
666
666
666
666
abc
abc
abc
abc
def
def
def
def
hij
hij
hij
hij
klm
klm
klm
klm
now please which element you want to search?
111
111 0
111 1
111 2
111 3
abc
abc 0
abc 1
abc 2
abc 3
klm
klm 0
klm 1
klm 2
klm 3
hij
hij 0
hij 1
hij 2
hij 3
^Z
请按任意键继续. . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值