当前位置: 首页 > news >正文

怎么给自己的公司做网站/黑马程序员培训机构官网

怎么给自己的公司做网站,黑马程序员培训机构官网,高端大气的网站模板,贵州三蒲建设工程有限公司网站1.概念 map中所有元素都是pair<key,value>&#xff0c;key 是map的键&#xff0c;value 是map的值 所有元素都会根据key自动排序 map/multimap属于关联式容器&#xff0c;底层结构是用二叉树实现。 map和multimap区别&#xff1a; map不允许容器中有重复key值元素 m…

1.概念

map中所有元素都是pair<key,value>,key 是map的键,value 是map的值

所有元素都会根据key自动排序

map/multimap属于关联式容器,底层结构是用二叉树实现。

map和multimap区别

  • map不允许容器中有重复key值元素

  • multimap允许容器中有重复key值元素

2.map构造和赋值

map<T1,T2> mp;

map(const map &mp);// 拷贝构造

map &operator=(const map &mp);

#include <iostream>
#include <map>using namespace std;void printMap(const map<int,int> &mp)
{for(map<int,int>::const_iterator it = mp.begin(); it != mp.end(); it++){cout << "key = " << it->first << " value = " << it->second << endl;}cout<<endl;
}void test(){map<int,int> m;m.insert(pair<int,int>(1,10));m.insert(pair<int,int>(2,20));m.insert(pair<int,int>(3,30));printMap(m);map<int,int>m2(m);// 拷贝构造printMap(m2);map<int,int>m3;m3 = m2;// 赋值printMap(m3);}int main()
{test();system("pause");return 0;
}

3.map大小和交换

#include <iostream>
#include <map>using namespace std;void printMap(map<int, int> &m){for (map<int, int>::iterator it = m.begin(); it != m.end(); it++){cout << "key = " << it->first << " value = " << it->second << endl;}cout<<endl;
}
void test(){map<int,int> m;m.insert(pair<int,int>(1,10));m.insert(pair<int,int>(2,20));m.insert(pair<int,int>(3,30));if (m.empty()){cout<<"map为空"<<endl;}else{cout<<"map不为空"<<endl;cout<<"map的大小为:"<<m.size()<<endl;}map<int,int>m2(m);map<int,int>m3;m3.insert(pair<int,int>(4,40));m3.insert(pair<int,int>(5,50));m3.insert(pair<int,int>(6,60));m3.swap(m2);printMap(m3);}int main()
{test();system("pause");return 0;
}

4.map插入和删除

insert(elem);

clear();

erase(pos);// 删除pos位置的元素,返回删除元素的下一个位置

erase(beg,end);// 删除beg到end之间的元素,左闭右开

erase(key);// 删除key对应的元素

#include <iostream>
#include <map>using namespace std;void printMap(const map<int,int> &m){for(map<int,int>::const_iterator it=m.begin();it!=m.end();it++){cout<<it->first<<" "<<it->second<<endl;}cout<<endl;
}void test(){map<int,int> m;m.insert(pair<int,int>(1,10));m.insert(make_pair(2,20));m.insert(map<int,int>::value_type(3,30));m[4]=40;printMap(m);//删除m.erase(m.begin());printMap(m);m.erase(3);printMap(m);//清空m.clear();m.erase(m.begin(),m.end());printMap(m);
}int main()
{test();system("pause");return 0;
}

5. map查找和统计

find(key);// 查找key对应的value,不存在返回end()

count(key);// 统计key出现的次数

#include <iostream>
#include <map>using namespace std;void printMap(map<int, int> &m)
{for (map<int, int>::iterator it = m.begin(); it != m.end(); it++){cout << "key = " << it->first << " value = " << it->second << endl;}cout<<endl;
}void test(){map<int, int>m; m.insert(pair<int, int>(1, 10));m.insert(pair<int, int>(2, 20));m.insert(pair<int, int>(3, 30));auto pos=m.find(3);// auto自动推导迭代器类型if(pos!=m.end()){cout<<"key = "<<pos->first<<" value = "<<pos->second<<endl;}else{cout<<"没有找到key = 3"<<endl;}int num=m.count(1);cout<<"key = 1 出现的次数 = "<<num<<endl;
}int main()
{test();system("pause");return 0;
}

 6. map容器排序

map容器默认排序规则为 按照key值进行 从小到大排序

#include <iostream>
#include <map>using namespace std;class MyCompare{public:bool operator()(int v1,int v2) const{return v1>v2;}
};void test(){map<int, int, MyCompare> m;m.insert(make_pair(1, 10));m.insert(make_pair(2, 20));m.insert(make_pair(3, 30));m.insert(make_pair(4, 40));m.insert(make_pair(5, 50));for (map<int, int,MyCompare>::const_iterator it = m.begin(); it != m.end(); it++){cout << "key = " << it->first << " value = " << it->second << endl;}
}int main()
{test();system("pause");return 0;
}

http://www.whsansanxincailiao.cn/news/31941156.html

相关文章:

  • 网页制作正版网站/百度手机应用市场
  • 购物网站建设得背景/友情链接举例
  • 水平b2b电子商务网站有哪些/网络营销公司全网推广公司
  • 做调查赚钱靠谱的网站有哪些/手机网站百度关键词排名查询
  • 传奇游戏排行榜/优化网站技术
  • 网站建设公司工作流程/爱站网关键词查询网站的工具
  • 阿里云网站备案好了 怎么建站/泉州关键词优化软件
  • 1688网站批发/google手机官网
  • 东莞网站建设 餐饮/舆情通
  • dede门户网站模板/厨师培训学校
  • 东莞模板建站哪家好/sem竞价课程
  • 婚恋网站模板下载/外贸商城建站
  • 网站投放广告怎么做/wordpress建站公司
  • 网站怎么做按钮/手机百度网盘网页版登录入口
  • 网站制作 杭州公司/优化课程
  • 西安做网站公司必达/网络营销公司排行榜
  • web网站开发里怎么切换界面/神马搜索seo优化排名
  • 昆明网站建设外包/东莞网络优化调查公司
  • 网站源码上传到空间以后怎么做/交换链接的其它叫法是
  • 做网站浏览器/网站优化seo怎么做
  • 山东嘉邦家居用品公司网站 加盟做经销商多少钱 有人做过吗/互联网营销培训平台
  • 网站群管理平台建设/合肥seo整站优化网站
  • 品牌网站建设流程图/重庆seo多少钱
  • 光谷网站建设公司/我是站长网
  • 网站集约化建设的总体情况/网站建设及推广优化
  • 网站排名不可有利就前/百度招商加盟推广
  • 群英云服务器/江阴网站优化公司
  • 网站是怎么做/肇庆seo按天收费
  • 怎么做亚马逊网站/产品推广图片
  • 外贸网站支付系统/网络优化排名培训