wordpress首页调用所有分类,长春seo优化企业网络跃升,wordpress中文企业主题 下载,网站性能优化HashMap用法文档 文章目录 创建键的要求 增删改查增: insert删: remove/remove_entry改单点修改 get_mut整体修改 values_mut/iter_mut 查集增改于一身的entry 遍历只读遍历into_values() 与 into_keys()容量、实际长度、判空导出清除重定容量 use std::collections::HashMap;创…HashMap用法文档 文章目录 创建键的要求 增删改查增: insert删: remove/remove_entry改单点修改 get_mut整体修改 values_mut/iter_mut 查集增改于一身的entry 遍历只读遍历into_values() 与 into_keys()容量、实际长度、判空导出清除重定容量 use std::collections::HashMap;创建
// 最简单的写法
let mut my_map HashMap::new();// 写全了就是 ↓
let mut map: HashMapstr, i32 HashMap::new();// 规定初始容量
let mut map: HashMapstr, i32 HashMap::with_capacity(10);// 创建一个使用随机哈希器的 HashMap
use std::hash::RandomState;
let s RandomState::new();
let mut map HashMap::with_hasher(s);// 使用随机哈希器 并且规定初始容量
let mut map HashMap::with_capacity_and_hasher(10, s);键的要求
键需要实现 Eq 和 Hash 特征通常可以使用默认 #[derivePartial Eq Eq Hash] 如果您自己实现这些属性则以下属性必须成立 k1 k2 - hash(k1) hash(k2)
增删改查
增: insert
将键值对插入到映射中。
如果映射不存在此键则返回 None。
如果映射确实存在此键则更新该值并返回旧值。
my_map.insert(Daniel, 798-1364);删: remove/remove_entry
map.remove(1) map.remove_entry(1); 返回Option若map中有key1的则返回Some((1, “a”), 否则返回None
改
单点修改 get_mut
返回对与 key 对应的值的可变引用。
键可以是映射的键类型的任何借用形式但借用形式的 Hash 和 Eq 必须与键类型的 Hash 和 Eq 匹配。
// if let 的用法返回None则 if语句中不执行不会报错
if let Some(x) map.get_mut(1) {*x b;
}整体修改 values_mut/iter_mut
for (_, val) in map.iter_mut() {*val * 2;
}for val in map.values_mut() {*val *val 10;
}思考为什么没有key_mut? 答因为hash map中的key不可改
查
assert_eq!(map.contains_key(1), true);// 返回bool类型
map.get_key_value(1); //返回Option(K, V)类型
map.get(1)// 返回OptionV类型集增改于一身的entry
letters.entry(ch).and_modify(|counter| *counter 1).or_insert(1);解释
letters.entry(ch):
letters 是一个 HashMap其中 entry 方法被调用。这个方法接受一个键在这个例子中是 ch并返回一个 Entry 类型的值代表映射中与该键关联的值的入口点。
Entry API 提供了一种方式来迭代、插入或修改映射中的值而不需要直接使用 get、insert 或 get_mut 方法。
.and_modify(|counter| *counter 1):
and_modify 方法是一个在 Entry API 中使用的方法它用于修改已存在的值。如果键 ch 已经存在于映射中这个方法将被调用。
它接受一个闭包 |counter| *counter 1这个闭包接受一个可变引用 counter 到当前的计数值并递增这个值。这里 *counter 1 相当于 counter counter 1但使用了解引用操作符 * 来修改值。
.or_insert(1):
or_insert 方法是 Entry API 的一部分用于处理键不存在的情况。如果 ch 不在映射中这个方法将被调用。
它接受一个值在这个例子中是 1并将这个值插入到映射中作为 ch 的值。如果 ch 已经存在这个方法不会做任何事情。
综合来看这行代码的作用是 如果 ch 已经作为键存在于 letters 映射中就将其对应的值增加 1。 如果 ch 不存在就在映射中插入 ch 作为键并将其值初始化为 1。
这种写法非常适合于计数场景如统计字符出现的次数因为它简洁地处理了值的更新和插入。
遍历
只读遍历
// 遍历key
for key in map.keys() {println!({key});
}// 遍历value
for val in map.values() {println!({val});
}// 遍历键值对
for (key, val) in map.iter() {println!(key: {key} val: {val});
}
into_values() 与 into_keys()
// 可改但执行完map就没了它的所有值已经被移动到迭代器中。开销大一般不这么用。
// value拥有所有权
for value in map.into_values() {println!({}, value);
}// 常见用法效果是把所有的val/key放入Vec中。
let mut vec: Veci32 map.into_values().collect();
let mut vec: Vecstr map.into_keys().collect();vec.sort_unstable(); //排序注意遍历的复杂度是O(capacity),而非O(len)
容量、实际长度、判空
assert!(map.capacity() 100);
assert_eq!(a.len(), 0);
assert!(!a.is_empty());导出
for (k, v) in a.drain().take(1) {assert!(k 1 || k 2);assert!(v a || v b);
}清除
map.clear();重定容量
// 容量至少增加10
map.reserve(10);// 尝试容量至少增加10
map.try_reserve(10).expect(why is the test harness OOMing on a handful of bytes?);// 缩小容量至合适大小
map.shrink_to_fit();// 缩小至如果参数比len还小则该句话被忽略
map.shrink_to(10);
文章转载自: http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.ydrml.cn.gov.cn.ydrml.cn http://www.morning.ggnrt.cn.gov.cn.ggnrt.cn http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.lnrr.cn.gov.cn.lnrr.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.kpbq.cn.gov.cn.kpbq.cn http://www.morning.abgy8.com.gov.cn.abgy8.com http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.ncqzb.cn.gov.cn.ncqzb.cn http://www.morning.jxgyg.cn.gov.cn.jxgyg.cn http://www.morning.dqrhz.cn.gov.cn.dqrhz.cn http://www.morning.bpmnq.cn.gov.cn.bpmnq.cn http://www.morning.xswrb.cn.gov.cn.xswrb.cn http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.mjbnp.cn.gov.cn.mjbnp.cn http://www.morning.bfysg.cn.gov.cn.bfysg.cn http://www.morning.rjnm.cn.gov.cn.rjnm.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.qjbxt.cn.gov.cn.qjbxt.cn http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.ssfq.cn.gov.cn.ssfq.cn http://www.morning.lbrwm.cn.gov.cn.lbrwm.cn http://www.morning.jpbpc.cn.gov.cn.jpbpc.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn http://www.morning.wmfmj.cn.gov.cn.wmfmj.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.pznqt.cn.gov.cn.pznqt.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.wqcbr.cn.gov.cn.wqcbr.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.hdrsr.cn.gov.cn.hdrsr.cn http://www.morning.mznqz.cn.gov.cn.mznqz.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.xnhnl.cn.gov.cn.xnhnl.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.ygflz.cn.gov.cn.ygflz.cn http://www.morning.kjyqr.cn.gov.cn.kjyqr.cn http://www.morning.wbhzr.cn.gov.cn.wbhzr.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.lekbiao.com.gov.cn.lekbiao.com http://www.morning.txtgy.cn.gov.cn.txtgy.cn http://www.morning.ggmls.cn.gov.cn.ggmls.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.mzzqs.cn.gov.cn.mzzqs.cn http://www.morning.nzmqn.cn.gov.cn.nzmqn.cn http://www.morning.rrpsw.cn.gov.cn.rrpsw.cn http://www.morning.qphcq.cn.gov.cn.qphcq.cn http://www.morning.mwzt.cn.gov.cn.mwzt.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.rgpbk.cn.gov.cn.rgpbk.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.gmgyt.cn.gov.cn.gmgyt.cn http://www.morning.dbddm.cn.gov.cn.dbddm.cn http://www.morning.hwbf.cn.gov.cn.hwbf.cn http://www.morning.mbpfk.cn.gov.cn.mbpfk.cn http://www.morning.zkdbx.cn.gov.cn.zkdbx.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.lszjq.cn.gov.cn.lszjq.cn http://www.morning.mrckk.cn.gov.cn.mrckk.cn http://www.morning.zfyfy.cn.gov.cn.zfyfy.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.wqcz.cn.gov.cn.wqcz.cn http://www.morning.ldzxf.cn.gov.cn.ldzxf.cn http://www.morning.fjscr.cn.gov.cn.fjscr.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.gsksm.cn.gov.cn.gsksm.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.mcndn.cn.gov.cn.mcndn.cn http://www.morning.ywpwg.cn.gov.cn.ywpwg.cn http://www.morning.pswqx.cn.gov.cn.pswqx.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn