multimap-size

本文展示了一个使用 C++ 实现的多重映射 (multimap) 的示例程序,通过插入不同元素并显示其数量来说明 multimap 的基本用法。代码中定义了一个 ID 类用于存储标识符和名称,并使用多重映射存储整数键和 ID 对象值。
////////////////////////////////////////
//      2018/05/06 9:29:46
//      multimap-size

// the number of elements in the maltimap
#include <iostream>
#include <map>
#include <iomanip>
#include <string>

using namespace std;

template<class T>
class ID{
private:
    T Id, name;
public:
    ID(T t, T n) :Id(t), name(n){}
    void print(){
        cout.setf(ios::left);
        cout << setw(15) << name << " " << Id << endl;
        cout.unsetf(ios::left);
    }
};

//=======================================
int main(){
    typedef ID<string> Id;
    typedef multimap<int, Id> M;
    typedef M::value_type v_t;

    M m;
    m.insert(v_t(1,Id("000123","Shevchenko")));
    m.insert(v_t(2,Id("000124","Pushkin")));
    m.insert(v_t(3,Id("000125","Shakesperae")));
    // same key
    m.insert(v_t(3,Id("000126","Smith")));

    cout << "size of multimap 'm' = " << m.size() << endl;

    M::iterator it = m.begin();
    while (it != m.end()){
        cout.setf(ios::left);
        cout << setw(3) << it->first;
        it->second.print();
        it++;
    }
    return 0;
}

/*
OUTPUT:
    size of multimap 'm' = 4
    1  Shevchenko      000123
    2  Pushkin         000124
    3  Shakesperae     000125
    3  Smith           000126
*/ 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值