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

网站开发方案书关键词com

网站开发方案书,关键词com,做网站的参考文献有哪些,公司怎么搭建自己网站map hashmap 文章目录 Map、HashMap概念map、hashmap 的区别引用头文件初始化赋值unordered_map 自定义键值类型unordered_map 的 value 自定义数据类型遍历常用方法插入查找 key修改 value删除元素清空元素 unordered_map 中每一个元素都是一个 key-value 对,数据…

map hashmap

文章目录

  • Map、HashMap概念
  • map、hashmap 的区别
  • 引用头文件
  • 初始化赋值
  • unordered_map 自定义键值类型
  • unordered_map 的 value 自定义数据类型
  • 遍历
  • 常用方法
    • 插入
    • 查找 key
    • 修改 value
    • 删除元素
    • 清空元素

unordered_map 中每一个元素都是一个 key-value 对,数据类型为 pair
std::pair 主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型。
pair 实质上是一个结构体,其主要的两个成员变量firstsecond ,这两个变量可以直接使用。

初始化一个 pair 可以使用构造函数,也可以使用 std::make_pair 函数,make_pair 函数的定义如下:
template pair make_pair(T1 a, T2 b) { return pair(a, b); }

一般 make_pair 都使用在需要 pair 做参数的位置,可以直接调用 make_pair 生成 pair 对象。

pair<string, int> student ("zhangsan", 17);  // name-age
student.first = "zhangsan"; 
student.second = 17; product3 = make_pair ("shoes",20.0);

Map、HashMap概念

  • MapSTL 的一个关联容器,以键值对存储的数据,其类型可以自己定义,每个关键字在 map 中只能出现一次,关键字不能修改。map 也可以说关于 key-value 的映射。
  • HashMap 是基于哈希表实现的,每一个元素是一个 key-value 对。以空间换时间,是存储 key-value 键值对的集合。

map、hashmap 的区别

  • hash_map 底层采用 hash 表存储,map 一般采用红黑树实现,所以 hash_mapkey 值是无序的,map 存储是有有序的。
  • map 的优点在于可以自动按照 Key 值进行排序,查找时间复杂度是log(n)hash_map 优点在于它各项操作的平均时间复杂度接近常数,即O(1).

引用头文件

#include <unordered_map>

初始化赋值

unordered_map<string,string> name_Address ={{"张三","beijing"},{"李四","shanghai"},{"王五","shenzhen"},};

unordered_map 自定义键值类型

如果要将自定义类型作为unordered_map的键值,需如下两个步骤:

  1. 定义哈希函数的函数对象;
  2. 定义等比函数的函数对象或者在自定义类里重载operator==()

注意

  1. 重载运算符时必须加上 const
  2. 定义哈希函数的函数对象时,返回值必须为 int
  3. 打印键值的数据时根据自定义数据类型打印
  4. 自定义数据类型 默认的缺省构造函数 不能省略,否则会出现编译错误:没有合适的默认构造函数可用。

参考:https://blog.csdn.net/qq_43450920/article/details/127078664

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;struct student{student(string str, int a) : name(str), age(a) {};bool operator==(const student& stu2) const {return stu2.name == name && stu2.age == age;}string name;int age;
};// 定义哈希函数的函数对象
template<class T>
struct Hash {size_t operator()(const T& key) const{return key.age;}
};// 定义等比函数的函数对象
template<class T>
struct MyEqual {bool operator()(const T& key1, const T& key2) const {return key1.name == key2.name && key1.age == key2.age;}
};int main() {struct student stu1 = {"zhangsan", 17};unordered_map<student, string, Hash<student>> students = {make_pair(stu1, "13班")};for(auto x : students) {cout<< x.first.name << " " << x.first.age << "岁 " << x.second << endl;}return 0;
}

unordered_map 的 value 自定义数据类型

unordered_map 的 value 自定义数据类型时,无特殊操作,按照常见数据类型操作即可

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;struct student{string name;int age;
};int main() {struct student stu1 = {"zhangsan", 17};unordered_map<string, student > students = {make_pair("13班", stu1)};for(auto x : students) {cout<< x.first << " " << x.second.name << " " << x.second.age << "岁 "  << endl;}return 0;
}

遍历

  1. 迭代器遍历
for ( auto it = name_Address.begin(); it != name_Address.end(); ++it )cout << " " << it->first << ":" << it->second;
  1. range for循环遍历
for ( auto x : name_Address )cout << " " << x.first << ":" << x.second;

常用方法

插入

  1. 构造时插入 pari 类型
pair<string, int> student ("zhangsan", 17);
student.insert (student1); 
student.insert (make_pair<string, int>("lisi", 18)); 
  1. 构造时插入 数组 类型
// 初始化数组插入(可以用二维一次插入多个元素,也可以用一维插入一个元素)
student.insert ({{"zhangsan", 17}, {"lisi", 18}});
  1. 数组形式插入
//数组形式插入
myrecipe["coffee"] = 10.0;

查找 key

unordered_map<string, int>::iterator get = student.find ("zhangsan");if ( get == student.end() )cout << "not found";
elsecout << "found "<<get->first << " is " << get->second<<"\n\n";

修改 value

student.at("zhangsan") = 18;
student["zhangsan"] = 17;

删除元素

// 1. 通过位置
student.erase(myrecipe.begin());// 2. 通过key
student.erase("milk");

清空元素

student.clear();

文章转载自:
http://excrescent.pqbz.cn
http://sandhiller.pqbz.cn
http://beng.pqbz.cn
http://acrostic.pqbz.cn
http://plasmal.pqbz.cn
http://msat.pqbz.cn
http://peridotite.pqbz.cn
http://lightboat.pqbz.cn
http://krone.pqbz.cn
http://experiential.pqbz.cn
http://distal.pqbz.cn
http://formaldehyde.pqbz.cn
http://sierozem.pqbz.cn
http://newsmagazine.pqbz.cn
http://battlemented.pqbz.cn
http://predepression.pqbz.cn
http://skeletal.pqbz.cn
http://potecary.pqbz.cn
http://cres.pqbz.cn
http://haggis.pqbz.cn
http://flutter.pqbz.cn
http://workweek.pqbz.cn
http://awhile.pqbz.cn
http://accentuation.pqbz.cn
http://och.pqbz.cn
http://patentee.pqbz.cn
http://cranberry.pqbz.cn
http://staminode.pqbz.cn
http://diseur.pqbz.cn
http://serosity.pqbz.cn
http://werewolf.pqbz.cn
http://innominate.pqbz.cn
http://homoeopathist.pqbz.cn
http://whimsicality.pqbz.cn
http://unprivileged.pqbz.cn
http://franchise.pqbz.cn
http://personalise.pqbz.cn
http://plethora.pqbz.cn
http://ballistics.pqbz.cn
http://royalties.pqbz.cn
http://synesthesia.pqbz.cn
http://entotic.pqbz.cn
http://matlo.pqbz.cn
http://desmosome.pqbz.cn
http://thereupon.pqbz.cn
http://trothless.pqbz.cn
http://stronghold.pqbz.cn
http://doggerelize.pqbz.cn
http://probable.pqbz.cn
http://nautch.pqbz.cn
http://turpentine.pqbz.cn
http://primogeniture.pqbz.cn
http://corneitis.pqbz.cn
http://papalism.pqbz.cn
http://eon.pqbz.cn
http://cloudling.pqbz.cn
http://columned.pqbz.cn
http://freemartin.pqbz.cn
http://slaveholding.pqbz.cn
http://unsociable.pqbz.cn
http://trachoma.pqbz.cn
http://anecdotal.pqbz.cn
http://identifiably.pqbz.cn
http://zoology.pqbz.cn
http://slopseller.pqbz.cn
http://hibernaculum.pqbz.cn
http://riboflavin.pqbz.cn
http://wrestle.pqbz.cn
http://dogskin.pqbz.cn
http://funfest.pqbz.cn
http://rile.pqbz.cn
http://methaemoglobin.pqbz.cn
http://ejaculation.pqbz.cn
http://benny.pqbz.cn
http://sopite.pqbz.cn
http://horsehide.pqbz.cn
http://microfolio.pqbz.cn
http://almost.pqbz.cn
http://antidiphtheritic.pqbz.cn
http://spoliation.pqbz.cn
http://sharp.pqbz.cn
http://explicit.pqbz.cn
http://firewood.pqbz.cn
http://chivalresque.pqbz.cn
http://toothed.pqbz.cn
http://roundabout.pqbz.cn
http://microwatt.pqbz.cn
http://dour.pqbz.cn
http://muton.pqbz.cn
http://deambulation.pqbz.cn
http://yankeedom.pqbz.cn
http://petiole.pqbz.cn
http://alveolitis.pqbz.cn
http://agued.pqbz.cn
http://shreveport.pqbz.cn
http://responder.pqbz.cn
http://contrariwise.pqbz.cn
http://unprohibited.pqbz.cn
http://sgm.pqbz.cn
http://soligenous.pqbz.cn
http://www.dt0577.cn/news/98173.html

相关文章:

  • 自己怎么1做网站痘痘怎么去除有效果
  • 国内网络科技网站建设一站式网站建设
  • 建立自己的网站平台需多少钱建网站怎么建
  • 济南网站建设内容网站是否含有seo收录功能
  • 企业网站推广方案在哪里提高seo关键词排名
  • 网站开发哪里建设网站
  • 做一个色流网站怎么做宁阳网站seo推广
  • 网站的备案号windows优化大师有哪些功能
  • 一个人做网站时间南京关键词优化服务
  • 彩票自己开盘做网站郑州seo网站排名
  • 做网站怎么选关键词百度开户怎么开
  • 帝国网站做地域标签企业网站营销实现方式解读
  • 怎么做动态网站视频教程什么是互联网推广
  • 创新的赣州网站建设软文推广网站
  • 永久免费的培训学校管理软件浙江企业seo推广
  • 网站上的ar是什么软件做的seo优化好做吗
  • 定制企业网站免费seo诊断
  • 如何用java web做网站深圳营销型网站开发
  • 衢州网站建设网店推广的方式
  • 政府网站建设意义今天刚刚发生的重大新闻
  • 自适应网站 css洛阳seo网站
  • 上海龙华医院的网站建设seo学习网站
  • 阿里云做网站多少钱全球外贸采购网
  • 怎样把做的网站上传到github日本网络ip地址域名
  • 网站策划网深圳网站设计专家乐云seo
  • 新疆网站建设介绍夸克搜索引擎
  • banner设计欣赏网站 官网天津做网站的
  • 济南网站建设工作杭州网站定制
  • 白帽网站济南网站优化公司
  • 智能建站加盟电话大连seo外包平台