map<T1,T2> mp;
map(const map &mp);
map& operator=(三、map大小和交换
size();
empty();
swap(st);
四、插入和删除
insert(ele);
clear();
erase(pos);
erase(beg,end);
erase(key);
#include<iostream>
using namespace std;
#include <map>
//map容器 插入和删除
void printMap(map<int,int>&m)
{
for (map<int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int>m;
插入
第一种
m.insert(pair<int>(1,10));
第二种
m.insert(make_pair(2,1)">20第三种
m.insert(map<int>::value_type(3,1)">30第四种
m[4] = 40;
[]不建议插入,用途 可以利用key访问到value
cout << m[4] << endl;
printMap(m);
删除
m.erase(m.begin());
printMap(m);
m.erase(3); 按照key删除
printMap(m);
清空
m.erase(m.begin(),m.end());
m.clear();
printMap(m);
}
int main() {
test01();
system(pause");
return 0;
}
五、查找和统计
test01()
{
查找
map<m;
m.insert(pair<));
m.insert(pair<));
map<int>::iterator pos = m.find(3if (pos != m.end())
{
cout << 查到了元素 key = " << (*pos).first << " << pos->second << endl;
}
else
{
cout << 未找到元素" << endl;
}
统计
map不允许插入重复key 元素 ,count统计而言 结果要么是0 要么是1
multimap的count统计可能大于1
int num = m.count();
cout << num = " << num << endl;
}
六、map排序(按升序排序)
#include<iostream>
class MyCompare
{
public:
bool operator()(int v1,1)">int v2) const
{
降序
return v1 > v2;
}
};
map容器 排序
m;
m.insert(make_pair());
m.insert(make_pair(5,1)">504,1)">));
endl;
}
}
;
}
vs2019在重载operator()时需要用const修饰。
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!