multiset多重集合容器
multiset与set一样, 也是使用红黑树来组织元素数据的, 唯一不同的是, multiset允许重复的元素键值插入, 而set则不允许.
multiset也需要声明头文件包含"#include<set>", 由于它包含重复元素, 所以, 在插入元素, 删除元素, 查找元素上较set有差别.
 
1.1multiset元素的插入
下面这个程序插入了重复键值"123", 最后中序遍历了multiset对象.
#include<set>
#include<string>
#include<iostream>
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");
    multiset<string>::iterator it;
    for(it = ms.begin(); it != ms.end(); it++)
    {
        cout << *it << endl;
    }
    return 0;
}
/*
111
123
123
aaa
abc
*/
 
1.2multiset元素的删除
采用erase()方法可以删除multiset对象中的某个迭代器位置上的元素, 某段迭代器区间中的元素, 键值等于某个值的所有重复元素, 并返回删除元素的个数. 采用clear()方法可以清空元素.
下面这个程序说明了insert()方法的使用方法:
#include<set>
#include<string>
#include<iostream>
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");
    multiset<string>::iterator it;
    for(it = ms.begin(); it != ms.end(); it++)
    {
        cout << *it << endl;
    }
    //删除值为"123"的所有重复元素, 返回删除元素的总数2
    int n = ms.erase("123");
    cout << "Total Delete : " << n << endl;
    //输出删除后的剩余元素
    cout << "all elements after deleted : " << endl;
    for(it = ms.begin(); it != ms.end(); it++)
    {
        cout << *it << endl;
    }
    return 0;
}
 
/*
111
123
123
aaa
abc
Total Delete : 2
all elements after deleted :
111
aaa
abc
*/
 
1.3查找元素
使用find()方法查找元素, 如果找到, 则返回该元素的迭代器位置(如果该元素存在重复, 则返回第一个元素重复元素的迭代器位置); 如果没有找到, 则返回end()迭代器位置.
下面程序具体说明了find()方法的作用方法:
#include<set>
#include<string>
#include<iostream>
using namespace std;
 
int main()
{
    multiset<string> ms;
    ms.insert("abc");
    ms.insert("123");
    ms.insert("111");
    ms.insert("aaa");
    ms.insert("123");
    multiset<string>::iterator it;
    //查找键值"123"
    it = ms.find("123");
    if(it != ms.end()) //找到
    {
        cout << *it << endl;
    }
    else
    {
        cout << "not find it!" << endl;
    }
    it = ms.find("bbb");
    if(it != ms.end()) //找到
    {
        cout << *it << endl;
    }
    else
    {
        cout << "Not Find It!" << endl;
    }
    return 0;
}
 
/*
123
Not Find It!
*/

C++STL之multiset多重集合容器的更多相关文章

  1. multiset多重集合容器(常用的使用方法总结)

    关于C++STL中multiset集合容器的学习,看别人的代码一百遍,不如自己动手写一遍. multiset多重集合容器和set集合容器的使用方法大多相同,不同的是multiset多重集合容器允许重复 ...

  2. multiset多重集合容器

    跟set集合容器相比,multiset多重集合容器也使用红黑树组织元素,仅仅是multiset多重集合容器同意将反复的元素键值插入.元素的搜索依旧具有对数级的算法时间复杂度,find和equal_ra ...

  3. Multiset ------ 多重集合

    Multiset的中文名是多重集合,其实就是集合的扩展版.唯一的不同是集合中一个值只能出现一次,而多重集合中一个值可以出现多次. 粗略看了看MSDN,在STL中,multiset和set的成员函数声明 ...

  4. STL中的set集合容器进行集合运算:并、交、差实例

    集合容器的集合运算:并.交.差: #include "stdafx.h" #include <iostream> #include <set> #inclu ...

  5. multiset基础学习,可以有重复类型的多重集合容器

    #include <set> #include <iostream> using namespace std; struct Student { char *name; int ...

  6. C++STL之set集合容器

    set集合容器 set集合容器实现了红黑树(Red-Black Tree)的平衡二叉检索树的数据结构, 在 插入元素时, 它会自动调整二叉树的排列, 把该元素放到适当的位置, 以确保每个子树根节点的键 ...

  7. stl之multiset容器的应用

    与set集合容器一样,multiset多重集合容器也使用红黑树组织元素数据,仅仅是multiset容器同意将反复的元素健值插入.而set容器则不同意. set容器所使用的C++标准头文件set.事实上 ...

  8. C++ STL set集合容器

    汇总了一些set的常用语句,部分参考了这篇:http://blog.163.com/jackie_howe/blog/static/199491347201231691525484/ #include ...

  9. 2.8 C++STL set/multiset容器详解

    文章目录 2.8.1 引入 2.8.2 代码示例 2.8.3 代码运行结果 2.8.4 对组pair的补充 代码实例 运行结果 总结 2.8.1 引入 set/multiset容器概念 set和mul ...

随机推荐

  1. springMVC从前端接受boolean类型的属性失败的问题

    springMVC从前端接收到的实体又一个boolean类型的属性,发现不管前端怎么操作,后台接收到的值都是false. 问题出现原因: eclipse在给实体属性生成getter和setter的时候 ...

  2. Problem09 求完数

    题目:一个数如果恰好等于它的因子之和,这个数就称为"完数". 分析:例如6=1+2+3. 编程找出1000以内的所有完数. 假如整数n除以m,结果是无余数的整数,那么我们称m就是n ...

  3. mc01_IntelliJ IDEA安装与Java项目创建以及Tomcat配置

    IntelliJ IDEA安装与激活 下载地址:http://www.jetbrains.com/idea/ 安装下一步下一步即可,关键是注册激活,该部分分两个步骤: 1. 配置windows hos ...

  4. 前端面试题 ----css篇

    转载自https://www.cnblogs.com/zhangshuda/p/8465043.html,感谢原博主 1.css盒模型有哪些及区别content-box border-box padd ...

  5. python 基础内置函数表及简单介绍

    内建函数名 (表达形式) 主要作用 备注 abs(x) 返回一个X值得绝对值(x=int/float/复数) all(iterable) 如果 iterable 的所有元素均为 True(或 iter ...

  6. ssh RSA key变化后处理

    root@localhost:/# scp -r root@172.19.47.30:/home/linux-4.16.2-devm.1.2.aarch64.dongbo ./@@@@@@@@@@@@ ...

  7. Sublime_text 3 安装 Package Control

    作者博文地址:https://www.cnblogs.com/liu-shuai/ 一.自动安装: 1.通过快捷键 ctrl+` 或者 View > Show Console 菜单打开控制台 2 ...

  8. ThreadLocal 开启事务

    1.ThreadLocal该类提供了线程局部变量 2.分析原理: ThreadLocal内部有一个Map.Map的key是当前线程对象,value是一个Object对象. 模拟该类: public c ...

  9. activeMq 配置(一)

    基础知识补充 1.ActiveMQ从入门到精通(一)https://www.jianshu.com/p/ecdc6eab554c 2.ActiveMQ从入门到精通(二)https://www.jian ...

  10. [HZOI 2015]复仇的序幕曲

    [题目描述] 你还梦不梦痛不痛,回忆这么重你怎么背得动 ----序言 当年的战火硝烟已经渐渐远去,可仇恨却在阿凯蒂王子的心中越来越深 他的叔父三年前谋权篡位,逼宫杀死了他的父王,用铁血手腕平定了国内所 ...