STL常见用法整理_multiset

来源:互联网 发布:python 策略回测 编辑:程序博客网 时间:2024/05/29 04:55

多重集合容器multiset

头文件:#include<set>

遍历

/*    用前向迭代器将容器中的元素从小到大打印出来*/    -------------------------------------------------------- 遍历 multiset 容器元素#include <set>#include <iostream>using namespace std;int main(){    multiset<int> ms;    ms.insert(10);    ms.insert(13);    ms.insert(11);    ms.insert(19);    ms.insert(13);    ms.insert(19);    ms.insert(19);    // 打印数据    multiset<int>::iterator i, iend;    iend = ms.end();    for (i=ms.begin(); i!=iend; ++i)        cout << *i << ' ';    cout << endl;    return 0;}


反向遍历

反向遍历 multiset 容器/*    使用反向迭代器,将容器中的元素,进行反向遍历,最后打印出来*/-------------------------------------------------------- 反向遍历 multiset 容器#include <set>#include <iostream>using namespace std;int main(){    multiset<int>  ms;    ms.insert(9);    ms.insert(5);    ms.insert(4);    ms.insert(3);    ms.insert(7);    ms.insert(8);    ms.insert(6);    ms.insert(10);    ms.insert(10);    ms.insert(10);    ms.insert(4);    ms.insert(4);    // 反向遍历打印    multiset<int>::reverse_iterator ri, riend;    riend = ms.rend();    for (ri=ms.rbegin(); ri!=riend; ++ri)        cout << *ri << ' ';    cout << endl;    return 0;}

multiset 容器的元素搜索

multiset 容器的元素搜索/*    利用 multiset 容器的 find 和 equal_range 函数,搜索键值为 13 的元素*/-------------------------------------------------------- multiset 容器的元素搜索#pragma warning(disable:4786)#include <set>#include <iostream>using namespace std;int main(){    multiset<int> ms;    ms.insert(10);    ms.insert(13);    ms.insert(12);    ms.insert(11);    ms.insert(19);    ms.insert(13);    ms.insert(16);    ms.insert(17);    ms.insert(13);    // 打印所有元素    multiset<int>::iterator i, iend;    iend  = ms.end();    for (i=ms.begin(); i!=iend; ++i)        cout << *i << ' ';    cout << endl;    // find 搜索元素 19    int v = 19;    multiset<int>::iterator i_v = ms.find(v);    cout << *i_v << endl;    // equal_range 搜索元素 13    v = 13;    pair<multiset<int>::iterator, multiset<int>::iterator>  p = ms.equal_range(v);    cout << "大于等于" << v << "的第一个元素 ( X >= k ) 为:" << *p.first << endl;    cout << "大于" << v << "的第一个元素( x > k ) 为:" << *p.second << endl;    // 打印重复键值元素 13    multiset<int>::iterator  j;    cout << "键值为" << v << "的所有元素为:";    for (j=p.first; j!=p.second; ++j)        cout << *j << ' ';    cout << endl << endl;    return 0;}


multiset 的其他函数用法

multiset 的其他函数用法/*    下面的示例程序以学生记录为元素插入 multiset 容器,比较函数是以年龄作比较,利用 size 和 count 函数统计了元素个数和某个键值下的元素个数*/-------------------------------------------------------- multiset 的其他函数用法#pragma warning(disable:4786)#include <set>#include <iostream>using namespace std;// 学生结构体struct Student{    char* name;    int year;    char* addr;};// 比较函数struct StudentLess{    bool operator()(const Student& s1, const Student& s2) const    {        return s1.year < s2.year;  // 比较学生年龄    }};int main(){    Student stuArray[] = {        {"张三", 23, "北京"},        {"李四", 24, "浙江"},        {"王五", 25, "上海"},        {"何亮", 22, "武汉"},        {"何生亮", 23, "深圳"}    };    // 创建 multiset 对象 ms    multiset<Student, StudentLess>  ms(stuArray, stuArray + 5, StudentLess());    // 统计    cout << "学生人数:" << ms.size() << endl << endl;    cout << "年龄为21岁的学生人数:" << ms.count(stuArray[0]) << endl << endl;    // 打印元素    multiset<Student, StudentLess>::iterator i, iend;    iend = ms.end();    cout << "姓名    " << "年龄    " << "地址    \n";    for (i=ms.begin(); i!=iend; ++i)        cout << (*i).name << "    " << (*i).year << "    " << (*i).addr << endl;    cout << endl;    return 0;}/*    从 count 函数输出可看到,真正作为键值的是 Student 结构体中的 year 变量值,而不是一个 Student 元素。因此,虽然 count(stuArray[0]) 传递的是一个 stuArray[0] 元素,但并不是统计等于 stuArray[0] 的元素个数,而是统计等于 stuArray[0].year 的元素个数,即年龄为 21 岁的学生人数为 2。*/



用multiset输出最小值最大值的例子。

/*

 输入格式

第行:一个正整数N(N≤,000),表示总共的天数。

第2 行到第N+1 行:每一行描述一天中收到的帐单。先是一个非负整数M,表示当天

收到的账单数,后跟M个正整数(都小于,000,000,000),表示每张帐单的面额。

输入数据保证每天都可以支付两张帐单,并且帐单会在最后一天全部付清。

输出格式

输出共N 行,每行两个用空格分隔的整数,分别表示当天支付的面额最小和最大的支

票的面额。

 */

#include <iostream>

#include <set>

using namespace std;

 

multiset<int> bills;    //multiset 有序的set

 

int main()

{

    freopen("input.txt","r",stdin);

    int n,a,m;

    cin>>n;

    for(int i=0;i<n;i++)

    {

        cin>>m;

        for(int j=0;j<m;j++)

        {

            cin>>a;

            bills.insert(a);

        }

        cout<<*bills.begin()<<" "<<*--bills.end()<<endl;  //取出最小值(第一个元素)和最大值(最后又个元素) 

        bills.erase(bills.begin());

        bills.erase(--bills.end());  //bills.end()指向最后一个的下一个为空

    }

    return 0;

}








0 0
原创粉丝点击