multiset多重集合容器

与set容器相比允许重复键值的插入

#include<set>
#include<string>
#include<iostream>
#include<cstdio>

using namespace std;

int main(){
    ///定义元素类型为string的多重集合对象s,当前没有任何元素
    multiset<string> ms;
    ms.insert("abc");
    ms.insert("123");
    ms.insert("111");
    ms.insert("aaa");
    ms.insert("123");
    for(auto it = ms.begin();it != ms.end();it++)
        cout << *it << endl;
    printf("\n");

    ///multiset中元素的删除
    ///删除值为“123”的所有重复元素,返回删除总数为2
    int n = ms.erase("123");
    cout << "Total deleted:" << n << endl;
    cout << "all element after deleted:" << endl;
    for(auto it = ms.begin();it != ms.end();it++)
        cout << *it << endl;
    printf("\n");

    ///查找元素
    ms.insert("123");
    ms.insert("123");
    ///查找键值“123”
    auto it = ms.find("123");
    if(it != ms.end())
        cout << *it << endl;
    else
        cout << "该元素不存在" << endl;
    it = ms.find("ddd");
    if(it != ms.end())
        cout << *it << endl;
    else
        cout << "该元素不存在" << endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值