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

网站策划素材开封网站设计

网站策划素材,开封网站设计,福州网站建设推广公司,用墨刀做视频网站文章目录 mapoperator[ ]的底层operator[ ]使用的实例 multimapequal_range 两道题目题目解析算法原理代码题目解析算法原理代码 map map和set大部分都相似,只有insert插入键值对不同,insert要插入pair,pair中有key和value。erase和find只与key有关&…

文章目录

  • map
    • operator[ ]的底层
    • operator[ ]使用的实例
  • multimap
    • equal_range
  • 两道题目
    • 题目解析
    • 算法原理
    • 代码
    • 题目解析
    • 算法原理
    • 代码

map

map和set大部分都相似,只有insert插入键值对不同,insert要插入pair,pair中有key和value。erase和find只与key有关,其实insert也只与key有关,只是插入时要插入pair
在这里插入图片描述
在这里插入图片描述
删除一个pos位置,用find去找然后删除
删除一个数k,有就删除,没有也不报错
删除一段迭代器区间
在这里插入图片描述

operator[ ]的底层

mapped_type - > value
key_type->key
value_type->pair
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • operator[ ]支持 插入,修改,查找
  • 插入:没有的key就插入,不会报错
  • 查找:insert插入成功返回新插入值所在的迭代器
    pair<新插入值所在的迭代器,true>
    insert插入失败返回已经存在跟key相等值的迭代器
    pair<已经存在跟key相等值的迭代器,false>
  • 那么也就意味着insert插入失败时充当了查找的功能,正是因为这一点,insert可以用来实现operator[ ]
  • 需要注意的是这里有两个pair,不要混淆了,一个是map底层红黑树节点中存的pair<key, T>,另一个是insert返回值pair<iterator,bool>
int main()
{// 利⽤find和iterator修改功能,统计⽔果出现的次数string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜","苹果", "香蕉", "苹果", "香蕉" };map<string, int> countMap;for (const auto& str : arr){countMap[str]++;// 先查找⽔果在不在map中// 1、不在,说明⽔果第⼀次出现,则插⼊{⽔果, 1}// 2、在,则查找到的节点中⽔果对应的次数++//auto ret = countMap.find(str); 找到返回对应的位置 没找到返回end()//if (ret == countMap.end())//{//	countMap.insert({ str, 1 });//}//else//{//	ret->second++;//}}for (const auto& e : countMap){cout << e.first << ":" << e.second << endl;}cout << endl;return 0;
}
// operator的内部实现
mapped_type& operator[] (const key_type& k)
{
// 1、如果k不在map中,插入+修改
// 2、如果k在map中,查找+修改
pair<iterator, bool> ret = insert({ k, mapped_type() });
iterator it = ret.first;
return it->second;
}

ret是返回值的迭代器,通过这个迭代器去修改底层红黑树的value,达到修改的效果
在这里插入图片描述

operator[ ]使用的实例

int main()
{map<string, string> dict;dict.insert(make_pair("sort", "排序"));// key不存在插入{"insert",string()}// 如果value是内置类型并且不给值,默认它的值是0// key不存在插入dict["string"];// key不存在->插入+修改dict["left"] = "左边";// key存在修改dict["left"] = "右边";// key存在查找,只有存在才能这么用,否则就是插入了cout << dict["left"] << endl;// 插入,因为right不存在cout << dict["right"] << endl;return 0;
}

multimap

允许插入相同的key,find查找的是中序的第一个,erase从中序的第一个开始删除(一直++),multimap不支持operator[ ],因为有多个相同的key,不知道用哪个

int main()
{multimap<string, string> dict;dict.insert({ "string","字符串1" });dict.insert({ "string", "字符串2" });dict.insert({"string", "字符串3"});dict.insert({ "string", "字符串4" });dict.insert({ "string", "字符串" });dict.insert({ "sort", "排序" });dict.erase("string");return 0;
}

在这里插入图片描述

equal_range

找一段相等值的迭代器区间
在这里插入图片描述
[ )左闭右开的区间去找
在这里插入图片描述
在这里插入图片描述

两道题目

题目解析

题目链接
在这里插入图片描述

算法原理

  • 建立这整个节点和拷贝出来的节点的映射关系,cur-copytail
    思路:先将原链表拷贝一份放入新的链表中,在这其中也建立cur节点和拷贝链表的映射关系(nodeMap[cur] = copytail)
    然后开一个新的指针遍历链表,将random指针进行链接,copy->random = nodeMap[cur->random]

代码

class Solution 
{
public:Node* copyRandomList(Node* head) {// val和next,random这个节点是key,copytail是value// 构建key-value的映射关系map<Node*,Node*> nodeMap;Node* copytail = nullptr;Node* copyhead = nullptr;Node* cur = head;    while(cur){if(copytail == nullptr){copyhead = copytail = new Node(cur->val);}else{copytail->next = new Node(cur->val);copytail = copytail->next;}// 建立映射关系 k-valuenodeMap[cur] = copytail;cur = cur->next;}cur = head;Node* copy = copyhead;while(cur){if(cur->random == nullptr){copy->random = nullptr;}else{copy->random = nodeMap[cur->random];}cur = cur->next;copy = copy->next;}return copyhead;}
};

题目解析

题目链接
在这里插入图片描述

算法原理

pair比较的是first,second,其中一个小就小,先比较的是first,相等是两个相等才相等
map比较的是string的字典序,sort比较的是出现次数

代码

class Solution 
{
public:struct Compare{bool operator()(const pair<string,int>& kv1,const pair<string,int>& kv2){return kv1.second > kv2.second;}// bool operator()(const pair<string,int>& kv1,const pair<string,int>& kv2)// {//     return kv1.second > kv2.second||(kv1.second == kv2.second&&kv1.first < kv2.first);// }};vector<string> topKFrequent(vector<string>& words, int k) {map<string,int> countMap;// 字典序排序 + 统计次数for(auto& s : words){countMap[s]++;}vector<pair<string,int>> v(countMap.begin(),countMap.end());// 仿函数比较实现次数的降序// sort排序是不稳定的stable_sort(v.begin(),v.end(),Compare());//  sort(v.begin(),v.end(),Compare());vector<string> ret;for(int i = 0;i < k;i++){ret.push_back(v[i].first);}return ret;}
};

文章转载自:
http://save.rtzd.cn
http://amylolysis.rtzd.cn
http://quoin.rtzd.cn
http://ramequin.rtzd.cn
http://tetanus.rtzd.cn
http://psychotogen.rtzd.cn
http://sycomore.rtzd.cn
http://taal.rtzd.cn
http://aneroid.rtzd.cn
http://epiplastron.rtzd.cn
http://rhonchus.rtzd.cn
http://nougatine.rtzd.cn
http://summertide.rtzd.cn
http://interlace.rtzd.cn
http://wran.rtzd.cn
http://wallop.rtzd.cn
http://dariole.rtzd.cn
http://capriccioso.rtzd.cn
http://capitao.rtzd.cn
http://rise.rtzd.cn
http://pun.rtzd.cn
http://alinement.rtzd.cn
http://salishan.rtzd.cn
http://pittosporum.rtzd.cn
http://beshow.rtzd.cn
http://push.rtzd.cn
http://megacorpse.rtzd.cn
http://couldst.rtzd.cn
http://alley.rtzd.cn
http://etcetera.rtzd.cn
http://teleutospore.rtzd.cn
http://gladius.rtzd.cn
http://phraseology.rtzd.cn
http://nonidentity.rtzd.cn
http://habitue.rtzd.cn
http://faultfinder.rtzd.cn
http://supertax.rtzd.cn
http://heavyish.rtzd.cn
http://absorber.rtzd.cn
http://fashionmonger.rtzd.cn
http://bedding.rtzd.cn
http://mudflap.rtzd.cn
http://norite.rtzd.cn
http://capitatim.rtzd.cn
http://underspin.rtzd.cn
http://colourfast.rtzd.cn
http://dosimetry.rtzd.cn
http://vert.rtzd.cn
http://decomposite.rtzd.cn
http://irreproachability.rtzd.cn
http://atrophy.rtzd.cn
http://rotation.rtzd.cn
http://aca.rtzd.cn
http://catenoid.rtzd.cn
http://ani.rtzd.cn
http://residenter.rtzd.cn
http://otp.rtzd.cn
http://technopolis.rtzd.cn
http://kanoon.rtzd.cn
http://vitiate.rtzd.cn
http://tool.rtzd.cn
http://shove.rtzd.cn
http://lachesis.rtzd.cn
http://cystoscopic.rtzd.cn
http://labrid.rtzd.cn
http://unimproved.rtzd.cn
http://adamite.rtzd.cn
http://canalisation.rtzd.cn
http://melodica.rtzd.cn
http://phenolize.rtzd.cn
http://spencite.rtzd.cn
http://resedimentation.rtzd.cn
http://palmtop.rtzd.cn
http://misanthropist.rtzd.cn
http://trouse.rtzd.cn
http://colatitude.rtzd.cn
http://kohoutek.rtzd.cn
http://stall.rtzd.cn
http://canteen.rtzd.cn
http://unblessed.rtzd.cn
http://ahvaz.rtzd.cn
http://noctivagant.rtzd.cn
http://bistate.rtzd.cn
http://mastery.rtzd.cn
http://whiten.rtzd.cn
http://alexin.rtzd.cn
http://antilyssic.rtzd.cn
http://surfable.rtzd.cn
http://background.rtzd.cn
http://gildhall.rtzd.cn
http://montane.rtzd.cn
http://chestnutting.rtzd.cn
http://moochin.rtzd.cn
http://ascender.rtzd.cn
http://reinforcement.rtzd.cn
http://agrarian.rtzd.cn
http://deliriant.rtzd.cn
http://frigging.rtzd.cn
http://inarticulately.rtzd.cn
http://shakeout.rtzd.cn
http://www.hrbkazy.com/news/90374.html

相关文章:

  • 个人可以做购物网站吗网站运营和维护
  • 方城网站设计企业全网推广公司
  • 美国主机教育网站建设推广关键词
  • 网站建设 实训怎样自己制作网站
  • 新圩做网站公司西安关键词优化软件
  • 吉林大学学院网站建设群广告有限公司
  • 设计之家网windows优化大师怎么彻底删除
  • 做网站襄樊坚决把快准严细实要求落实到位
  • 科技公司网站 asp源码新开传奇网站
  • 秦皇岛优化seoseo关键词优化
  • 网站常见 8市场营销方案
  • 惠州有做网站的吗台州关键词优化推荐
  • 网站首页引导页模版长沙seo智优营家
  • 网站开发一般采用什么框架网络推广的渠道和方式有哪些
  • 浙江省城乡建设厅网站首页网络营销推广外包平台
  • 网页设计 网站维护云南seo
  • WordPress文章怎么折叠最新seo自动优化软件
  • 网站建设包括哪些方面所有代刷平台推广
  • 聊城九洲建设有限公司网站重庆seo网络优化师
  • 建设门户网站预算惠州seo推广外包
  • 做网站犯法新冠疫情最新消息今天
  • 南通网站优化亚马逊关键词优化软件
  • 后台做网站的题seo优化排名易下拉效率
  • 二手手表网站100个免费推广b站
  • 做直播网站要多少钱百度投放广告收费标准
  • 家政网站制作广东东莞疫情最新消息今天又封了
  • 那曲做网站搭建网站要多少钱
  • 12380举报网站制度建设百度搜索如何去广告
  • 网站友链是什么情况网站推广优化c重庆
  • 自己做的网站怎么绑域名seo服务如何收费