////////////////////////////////////////
// 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
*/
multimap-size
最新推荐文章于 2024-03-12 11:03:37 发布
本文展示了一个使用 C++ 实现的多重映射 (multimap) 的示例程序,通过插入不同元素并显示其数量来说明 multimap 的基本用法。代码中定义了一个 ID 类用于存储标识符和名称,并使用多重映射存储整数键和 ID 对象值。
991

被折叠的 条评论
为什么被折叠?



