zoukankan      html  css  js  c++  java
  • C++STL之multiset多重集合容器

    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!
    */
  • 相关阅读:
    CVPR顶会热词统计
    @Annotation学习
    把一张表已有的数据对另一张表数据进行修改
    两张表数据不一致进行对比
    学习借鉴
    借鉴tcp
    借鉴tcp
    osi七层
    http学习
    Json学习
  • 原文地址:https://www.cnblogs.com/mjn1/p/10311785.html
Copyright © 2011-2022 走看看