现在的位置: 首页 > 综合 > 正文

multimap

2013年09月06日 ⁄ 综合 ⁄ 共 1606字 ⁄ 字号 评论关闭

// multimap.cpp : 定义控制台应用程序的入口点。
//multimap
提供多重映射,第一个值为key,可以重复,第二个为value

#include "stdafx.h"
#include <stdlib.h>
#include <map>
#include <iostream>
#include <string>
using namespace std;

class CStudent
{
public:
 CStudent(int index,string name,string sex,string address,int phoneNum)
 {
  this->m_index=index;
  this->m_name=name;
  this->m_sex=sex;
  this->m_address=address;
  this->m_phoneNumber=phoneNum;
 }
public:
 int m_index;
 string m_name;
 string m_sex;
 string m_address;
 int m_phoneNumber;
};
int _tmain(int argc, _TCHAR* argv[])
{
 cout<<"--------------mutimap test------------------"<<endl<<endl;

 
 CStudent *stu1=new CStudent(1,"张三","男","北京",123);
 CStudent *stu2=new CStudent(2,"李四","男","天津",345);
 CStudent *stu3=new CStudent(3,"王五","男","安徽",456);

 pair<int,CStudent> p1(1,*stu1);
 pair<int,CStudent> p2(1,*stu2);
 pair<int,CStudent> p3(2,*stu3);

 multimap<int,CStudent> mapStu;
 mapStu.insert(p1);
 mapStu.insert(p2);
 mapStu.insert(p3);

 typedef multimap<int,CStudent>::iterator stu_Map_it;
 pair<stu_Map_it,stu_Map_it> p=mapStu.equal_range(1);
 stu_Map_it i=mapStu.find(1);

 cout<<"key 为"<<i->first<<"的学生有"<<mapStu.count(1)<<"个 "<<"分别为:"<<endl;
 for(stu_Map_it k = p.first; k != p.second; k++) 
 { 
  cout<<k->second.m_index<<"  "<<k->second.m_name<<" "<<k->second.m_address<<" "<<k->second.m_sex<<" "<<k->second.m_phoneNumber<<endl; 

 } 
 cout<<"-------------------删除key重复的学生后----------------"<<endl;
 mapStu.erase(i);
 
 cout<<"学生总是为:"<<mapStu.size()<<endl;

 stu_Map_it it;
 for (it=mapStu.begin();it!=mapStu.end();it++)
 {
  cout<<it->second.m_index<<"  "<<it->second.m_name<<" "<<it->second.m_address<<" "<<it->second.m_sex<<" "<<it->second.m_phoneNumber<<endl; 

 }
 system("pause");
 return 0;
}

 

抱歉!评论已关闭.