stl multiset常用函数

    //构造函数
    //multiset()
    multiset<int> ms1;

    //指定一个比较函数来创建multiset对象
    struct KeyCompare
    {
        bool operator()(const char* s1, const char* s2) const
        {
            return strcmp(s1, s2) < 0;
        }
    };
    multiset<const char*, KeyCompare> ms2;

    //multiset(const _Myt& _Right)
    multiset<int> ms3(ms1);

    //multiset(_Iter _First, _Iter _Last)
    int arr[] = { 1, 1, 3, 42, 21, 31, 21 };
    multiset<int> ms4(arr, arr + 7);

    const char* szArray[] = { "hfdd", "do", "mmd" };
    multiset<const char*, KeyCompare> s5(szArray, szArray + 3, KeyCompare());        //do hfdd mmd

    //插入元素
    ms1.insert(2);        //2

    //在可能的位置之前插入元素
    ms1.insert(ms1.begin(), 7);        //2 7

    //插入区间元素
    int arr2[] = { 3, 9, 6 };
    ms1.insert(arr2, arr2 + 3);        //2 3 6 7 9

    //遍历元素
    multiset<int>::iterator iter;
    for (iter = ms1.begin(); iter != ms1.end(); ++iter)
    {
        cout << *iter << " ";
    }
    cout << endl;

    //反向遍历元素
    multiset<int>::reverse_iterator rIter;
    for (rIter = ms1.rbegin(); rIter != ms1.rend(); ++rIter)
    {
        cout << *rIter << " ";
    }
    cout << endl;

    multiset<int>::iterator rFind = ms1.find(7);
    if (rFind == ms1.end())
    {
        cout << "not find" << endl;
    }
    else
    {
        cout << "find" << endl;
    }

    //删除元素
    ms1.erase(ms1.begin());        //3 6 7 9
    //删除键值33的元素
    ms1.erase(33);                //3 6 7 9
    //删除区间元素
    ms1.erase(ms1.begin(), ++ms1.begin());    //6 7 9
    //删除所有元素
    ms1.clear();

    //容器是否为空
    bool b = ms1.empty();

    //实际元素个数
    size_t count = ms1.size();

    //键值等于7的元素的个数
    size_t num = ms1.count(7);

    //键值大于等于3的第一个元素的位置
    multiset<int>::iterator uIter = ms4.lower_bound(3);
    if (uIter == ms4.end())
    {
        cout << "not found" << endl;
    }
    else
    {
        int i = 0;
        iter = ms4.begin();
        while (iter != uIter)
        {
            ++i;
            ++iter;
        }
        cout << "find index:" << i << endl;        //2
    }

    //键值大于3的第一个元素的位置
    ms4.upper_bound(3);

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值