新建map
map<int,string> m;
map<string,string> m1;
插入数据
//插入数据方法1,这种方法若key已经存在,则不会插入数据
m.insert(pair<int,string>(3,"1a"));
m.insert(pair<int,string>(1,"a"));
m.insert(pair<int,string>(1,"c"));
//插入数据方法2,这种方法若key已经存在,则会覆盖旧的数据
m[2]="b";//下标是key,值是value
m[2]="b1";
//插入数据方法3。使用大括号将kv对括起来
m.insert({1,"c"});
//将字符串作为下标也是合法的,因为本质这个下标不是真正的下标
m1["a"]="b";
删除数据
//方法1,用迭代器删除
it=m.find(2);
if(it!=m.end()) m.erase(it);
//方法2,用关键字删除
m.erase(1);
修改数据
//方法1,通过迭代器修改
it=m.find(3);
if(it!=m.end()) it->second="dadasa";
//方法2,通过访问map元素修改,这个方法如果该key不存在的话会直接新建一个pair
m[3]="dasdasdasdasdsadsa";
查找数据
//查找
it=m.find(4);
遍历map
//正向迭代正向遍历
map<int,string>::iterator it;
for(it=m.begin();it!=m.end();it++)
cout<<it->first<<" "<<it->second<<endl;
map<string,string>::iterator it1;
for(it1=m1.begin();it1!=m1.end();it1++)
cout<<it1->first<<" "<<it1->second<<endl;
//逆向迭代逆向遍历
map<int, string>::reverse_iterator rit;
for(rit=m.rbegin();rit!=m.rend();rit++)
cout<<rit->first<<" "<<rit->second<<endl;
获取map大小
//获取map的大小
cout<<m.size()<<endl;
map的排序问题
因为map默认使用小于号排序,即按照key从小到大的顺序进行排序,当对自定义结构体排序的时候,需要自定义排序函数。
//法1,在自定义的结构体里面重载小于号的逻辑
typedef struct stu{
int niD;
string strName;
bool operator < (stu const& _A) const {
//这个函数指定排序策略,按niD排序(从小到大),如果niD相等的话,按strName排序
//从小到大排序,true代表该stu比A小
if(niD < _A.niD) return true;
if(niD == _A.niD)
return strName.compare(_A.strName) < 0;
return false;
}
};
//法2,重载sort类的函数
class sort{
public:
bool operator() (stu const &A, stu const &B) const{
if(A.niD < B.niD)
return true;
if(A.niD == B.niD)
return A.strName.compare(B.strName) < 0;
return false;
}
};
测试程序:代表学生的结构体有两个成员变量id与name都是字符串类型,加入map时的排序逻辑为名字在字典中较大的排在前面,如果名字相同则比较id,id较小的排在前面
#include <iostream>
#include <map>
using namespace std;
typedef struct stu{
string id;
string name;
//形参一定是常量引用类型,后面方法括号后面一定要有const重载符
bool operator < (stu const& other) const{
if(name==other.name)
return id<other.id;
else
return name>other.name;
}
}stu;
int main(){
map<stu,int> m;
stu s1,s2,s3;
s1.id="zc001";
s2.id="zc002";
s3.id="zc003";
s1.name="name1";
s2.name="name3";
s3.name="name1";
m[s1]=100;
m[s2]=98;
m[s3]=96;
map<stu,int>::iterator it;
for(it=m.begin();it!=m.end();it++)
cout<<it->first.id<<" "<<it->first.name<<" "<<it->second<<endl;
return 0;
}
结果:
zc002 name3 98
zc001 name1 100
zc003 name1 96
其他操作
//判断map是否为空
m.empty();
//清空map
m.clear();
总代码
#include<iostream>
#include<map>
using namespace std;
int main(){
//新建一个map
map<int,string> m;
map<string,string> m1;
//插入数据方法1,这种方法若key已经存在,则不会插入数据
m.insert(pair<int,string>(3,"1a"));
m.insert(pair<int,string>(1,"a"));
m.insert(pair<int,string>(1,"c"));
//插入数据方法2,这种方法若key已经存在,则会覆盖旧的数据
m[2]="b";//下标是key,值是value
m[2]="b1";
//将字符串作为下标也是合法的,因为本质这个下标不是真正的下标
m1["a"]="b";
//map会根据key值自动排序,按照key升序排序
//正向迭代正向遍历
map<int,string>::iterator it;
for(it=m.begin();it!=m.end();it++)
cout<<it->first<<" "<<it->second<<endl;
map<string,string>::iterator it1;
for(it1=m1.begin();it1!=m1.end();it1++)
cout<<it1->first<<" "<<it1->second<<endl;
//获取map的大小
cout<<m.size()<<endl;
//逆向迭代逆向遍历
map<int, string>::reverse_iterator rit;
for(rit=m.rbegin();rit!=m.rend();rit++)
cout<<rit->first<<" "<<rit->second<<endl;
//查找
it=m.find(4);
if(it==m.end()) cout<<"未找到"<<endl;
else cout<<"value:"<<it->second<<endl;
it=m.find(3);
if(it==m.end()) cout<<"未找到"<<endl;
else cout<<"value:"<<it->second<<endl;
//删除元素
//方法1,用迭代器删除
it=m.find(2);
if(it!=m.end()) m.erase(it);
for(it=m.begin();it!=m.end();it++)
cout<<it->first<<" "<<it->second<<endl;
//方法2,用关键字删除
m.erase(1);
for(it=m.begin();it!=m.end();it++)
cout<<it->first<<" "<<it->second<<endl;
//方法1,通过迭代器修改
it=m.find(3);
it->second="dadasa";
for(it=m.begin();it!=m.end();it++)
cout<<it->first<<" "<<it->second<<endl;
//方法2,通过访问map元素修改
m[1]="dasdasdasdasdsadsa";
for(it=m.begin();it!=m.end();it++)
cout<<it->first<<" "<<it->second<<endl;
return 0;
}
参考博文:https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html