c++ stl multimap基本操作使用技巧详细介绍
C++ stl Multimap 和C++ stl map 很相似,但是MultiMap允许重复的元素。
C++ stl Multimap的基本操作类成员函数列表介绍如下:
begin()返回指向第一个元素的迭代器
clear()删除所有元素
count()返回一个元素出现的次数
empty()如果multimap为空则返回真
end()返回一个指向multimap末尾的迭代器
equal_range()返回指向元素的key为指定值的迭代器对
erase()删除元素
find()查找元素
get_allocator()返回multimap的配置器
insert()插入元素
key_comp()返回比较key的函数
lower_bound()返回键值>=给定元素的第一个位置
max_size()返回可以容纳的最大元素个数
rbegin()返回一个指向mulitmap尾部的逆向迭代器
rend()返回一个指向multimap头部的逆向迭代器
size()返回multimap中元素的个数
swap()请问在linux下面编程怎样查询stl类的成员函数
iis7站长之家两个multimaps
upper_bound()返回键值>给定元素的第一个位置
value_comp()返回比较元素value的函数
C++ stl Multimap使用方法代码举例1:
//multimap允许重复的键值插入容器
// **********************************************************
// * pair只包含一对数值:pair<int,char> *
// * map是一个集合类型,永远保持排好序的, *
// pair * map每一个成员就是一个pair,例如:map<int,char> *
// * map的insert()可以把一个pair对象作为map的参数,例如map<p> *
// ***********************************************************
#pragma warning(disable:4786)
#include<map>
#include<iostream>
using namespace std;
int main(void)
{
multimap<int,char*> m;
//multimap的插入只能用insert()不能用数组
m.insert(pair<int,char*>(1,"apple"));
m.insert(pair<int,char*>(1,"pear"));//apple和pear的价钱完全有可能是一样的
m.insert(pair<int,char*>(2,"banana"));
//multimap的遍历只能用迭代器方式不能用数组
cout<<"***************************************"<<endl;
multimap<int,char*>::iterator i,iend;
iend=m.end();
for(i=m.begin();i!=iend;i++)
{
cout<<(*i).second<<"的价钱是"
<<(*i).first<<"元/斤n";
}
cout<<"***************************************"<<endl;
//元素的反相遍历
multimap<int,char*>::reverse_iterator j,jend;
jend=m.rend();
for(j=m.rbegin();j!=jend;j++)
{
cout<<(*j).second<<"的价钱是"
<<(*j).first<<"元/斤n";
}
cout<<"***************************************"<<endl;
//元素的搜索find(),pair<iterator,iterator>equal_range(const key_type &k)const
//和multiset的用法一样
multimap<int,char*>::iterator s;
s=m.find(1);//find()只要找到一个就行了,然后立即返回。
cout<<(*s).second<<" "
<<(*s).first<<endl;
cout<<"键值等于1的元素个数是:"<<m.count(1)<<endl;
cout<<"***************************************"<<endl;
//删除 erase(),clear()
m.erase(1);
for(i=m.begin();i!=iend;i++)
{
cout<<(*i).second<<"的价钱是"
<<(*i).first<<"元/斤n";
}
return 0;
}
C++ stl Multimap指定某个键值,进行遍历代码举例2
可以使用lower_bound和upper_bound函数进行遍历,也可以使用函数equal_range。其返回的是一个游标对。游标对pair::first是由函数lower_bound得到
的x的前一个值,游标对pair::second的值是由函数upper_bound得到的x的后一个值。
multimap<int,int> a;
a.insert(pair<int,int>(1,11));
a.insert(pair<int,int>(1,12));
a.insert(pair<int,int>(1,13));
a.insert(pair<int,int>(2,21));
a.insert(pair<int,int>(2,22));
a.insert(pair<int,int>(3,31));
a.insert(pair<int,int>(3,32));
multimap<int,int>::iterator p_map;
pair<multimap<int,int>::iterator, multimap<int,int>::iterator> ret;
for(p_map = a.begin() ; p_map != a.end();)
{
cout<<p_map->first<<" =>";
ret = a.equal_range(p_map->first);
for(p_map = ret.first; p_map != ret.second; ++p_map)
cout<<""<< (*p_map).second;
cout<<endl;
}
C++ stl Multimap详细代码举例3
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef struct employee
{
//Member Function
public:
employee(long eID, string e_Name, float e_Salary);
//Attribute
public:
long ID; //Employee ID
string name; //Employee Name
float salary; //Employee Salary
}employee;
//创建multimap的实例,整数(职位编号)映射员工信息
typedef multimap<int, employee> EMPLOYEE_MULTIMAP;
typedef multimap<int, employee>::iterator EMPLOYEE_IT; //随机访问迭代器类型
typedef multimap<int, employee>::reverse_iterator EMPLOYEE_RIT; //反向迭代器类型
employee::employee(long eID, string e_Name, float e_Salary)
: ID(eID), name(e_Name), salary(e_Salary) {}
//函数名:output_multimap
//函数功能:正向输出多重映射容器里面的信息
//参数:一个多重映射容器对象
void output_multimap(EMPLOYEE_MULTIMAP employ)
{
EMPLOYEE_IT employit;
for (employit = employ.begin(); employit != employ.end(); employit++)
{
cout << (*employit).first << 't' << (*employit).second.ID
<< 't' << (*employit).second.name << 't' << (*employit).second.salary
<< 't' << endl;
}
}
//函数名:reverse_output_multimap
//函数功能:逆向输出多重映射容器里面的信息
//参数:一个多重映射容器对象
void reverse_output_multimap(EMPLOYEE_MULTIMAP employ)
{
EMPLOYEE_RIT employit;
for (employit = employ.rbegin(); employit != employ.rend(); employit++)
{
cout << (*employit).first << 't' << (*employit).second.ID
<< 't' << (*employit).second.name << 't' << (*employit).second.salary
<< 't' << endl;
}
}
int main(int argc, char *argv[])
{
EMPLOYEE_MULTIMAP employees; //多重映射容器实例
//下面四个语句分别构造一个员工对象插入到多重映射容器
//注意因为是多重映射,所以可以出现重复的键,例如下面的信息有两个职位编号为118的员工
employees.insert(EMPLOYEE_MULTIMAP::value_type(118, employee(100, "luojiafeng", 8000)));
employees.insert(EMPLOYEE_MULTIMAP::value_type(112, employee(101, "luojiahui", 6000)));
employees.insert(EMPLOYEE_MULTIMAP::value_type(113, employee(102, "luokaifeng", 10000)));
employees.insert(EMPLOYEE_MULTIMAP::value_type(118, employee(103, "xujinghua", 20000)));
//正序输出多重映射容器中的信息
cout << "职位编号" << "员工ID" << 't'
<< "姓名" << 't' << 't' << "工资" << endl;
output_multimap(employees);
//逆序输出多重映射容器中的信息
cout << "职位编号" << "员工ID" << 't'
<< "姓名" << 't' << 't' << "工资" << endl;
reverse_output_multimap(employees);
//输出容器内的记录条数
cout<< "共有" << employees.size() << "条员工记录" << endl;
return 0;
}