map相对于set的区别,map具有键值和实值,所有元素根据键值自动排序。pair的第一元素也被称为键值,第二元素被称为实值。map的底层机制为红黑树。
map的key不能重复,multimap可以。,不能任意该键值,可改实值。
生成随机数:
#include<time.h>
#include<stdlib.h>
srand(time(NULL));放置种子
int n=rand() % 3+1;随机1~3的数
#include <iostream>
#include <map>
using namespace std;
//map初始化
void text01()
{
//map容器模板参数,第一个key类型,第二个value类型
map<int,int> mymap;
///插入数据 ,map有自动排序功能,用insert加数据
//pair.first() key值,pair.second value值
//第一种
//mymap.insert(pair<int,int>(10,10));
pair<map<int,int>::iterator,bool> ret=mymap.insert(pair<int,int>(10,10));
if(ret.second)
{
cout<<"插入成功"<<endl;
}
else
{
cout<<"插入失败"<<endl;
}
ret=mymap.insert(pair<int,int>(10,10));
if(ret.second)
{
cout<<"插入成功"<<endl;
}
else
{
cout<<"插入失败"<<endl;
}
//第二种
mymap.insert(make_pair(20,20));
//第三种
mymap.insert(map<int,int>::value_type(30,30));
//第四种
mymap[40]=40;
mymap[10]=20;//如果发现key不存在,创建pair插入到map容器中,存在则修改key对应的value值
cout<<mymap[60]<<endl;//若通过【】方式访问map中不存在的元素 ,则生成该元素,key值默认为0;
//打印
for(map<int,int>::iterator it=mymap.begin();it!=mymap.end();it++)
{
//*it取出来的是一个pair
cout<<(*it).second<<endl;
}
}
void text02()
{
map<int,int> m1;
m1.insert(pair<int,int>(10,15));
map<int,int> m2(m1);
m1.swap(m2);
cout<<m1.size()<<endl;
cout<<m1.empty()<<endl;
}
//查找操作
void text03()
{
map<int,int> m;
m[10]=20;
m[20]=30;
m[30]=40;
//cout<<m.find(20)<<endl;//查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回map.end();
cout<<m.count(10)<<endl;//返回容器 中以key:10为键的队组个数,对map来说是0或1,对multimap来说可以大于1;
//cout<<m.lower_bound(20)<<endl;//返回第一个key小于20的迭代器
//cout<<m.upper_bound(10)<<endl;// 大于
//cout<<m.equal_range(20)<<endl; // 大于等于
}
class mykey
{
public:
mykey(int midex,int mid):midex(midex),mid(mid)
{
}
public:
int midex;
int mid;
};
class compare
{
public:
bool operator() (mykey m1,mykey m2) const
{
if(m1.midex!=m2.midex)
return m1.midex<m2.midex;
else
{
return m1.mid<m2.mid;
}
}
};
void text04()
{
multimap<mykey,int,compare> mymap;
mymap.insert(make_pair(mykey(1,2),10));
mymap.insert(make_pair(mykey(3,3),15));
mymap.insert(make_pair(mykey(1,1),13));
mymap.insert(make_pair(mykey(3,4),20));
for(map<mykey,int,compare>::iterator it=mymap.begin();it!=mymap.end();it++)
{
cout<<((*it).first).midex<<":"<<((*it).first).mid<<"-"<<(*it).second<<endl;
}
}
int main()
{
//text01();
//text02();
//text03();
text04();
return 0;
}
#include <iostream>
#include <map>
using namespace std;
//map初始化
void text01()
{
//map容器模板参数,第一个key类型,第二个value类型
map<int,int> mymap;
///插入数据 ,map有自动排序功能,用insert加数据
//pair.first() key值,pair.second value值
//第一种
//mymap.insert(pair<int,int>(10,10));
pair<map<int,int>::iterator,bool> ret=mymap.insert(pair<int,int>(10,10));
if(ret.second)
{
cout<<"插入成功"<<endl;
}
else
{
cout<<"插入失败"<<endl;
}
ret=mymap.insert(pair<int,int>(10,10));
if(ret.second)
{
cout<<"插入成功"<<endl;
}
else
{
cout<<"插入失败"<<endl;
}
//第二种
mymap.insert(make_pair(20,20));
//第三种
mymap.insert(map<int,int>::value_type(30,30));
//第四种
mymap[40]=40;
mymap[10]=20;//如果发现key不存在,创建pair插入到map容器中,存在则修改key对应的value值
cout<<mymap[60]<<endl;//若通过【】方式访问map中不存在的元素 ,则生成该元素,key值默认为0;
//打印
for(map<int,int>::iterator it=mymap.begin();it!=mymap.end();it++)
{
//*it取出来的是一个pair
cout<<(*it).second<<endl;
}
}
void text02()
{
map<int,int> m1;
m1.insert(pair<int,int>(10,15));
map<int,int> m2(m1);
m1.swap(m2);
cout<<m1.size()<<endl;
cout<<m1.empty()<<endl;
}
//查找操作
void text03()
{
map<int,int> m;
m[10]=20;
m[20]=30;
m[30]=40;
//cout<<m.find(20)<<endl;//查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回map.end();
cout<<m.count(10)<<endl;//返回容器 中以key:10为键的队组个数,对map来说是0或1,对multimap来说可以大于1;
//cout<<m.lower_bound(20)<<endl;//返回第一个key小于20的迭代器
//cout<<m.upper_bound(10)<<endl;// 大于
//cout<<m.equal_range(20)<<endl; // 大于等于
}
class mykey
{
public:
mykey(int midex,int mid):midex(midex),mid(mid)
{
}
public:
int midex;
int mid;
};
class compare
{
public:
bool operator() (mykey m1,mykey m2) const
{
if(m1.midex!=m2.midex)
return m1.midex<m2.midex;
else
{
return m1.mid<m2.mid;
}
}
};
void text04()
{
multimap<mykey,int,compare> mymap;
mymap.insert(make_pair(mykey(1,2),10));
mymap.insert(make_pair(mykey(3,3),15));
mymap.insert(make_pair(mykey(1,1),13));
mymap.insert(make_pair(mykey(3,4),20));
for(map<mykey,int,compare>::iterator it=mymap.begin();it!=mymap.end();it++)
{
cout<<((*it).first).midex<<":"<<((*it).first).mid<<"-"<<(*it).second<<endl;
}
}
int main()
{
//text01();
//text02();
//text03();
text04();
return 0;
}