【C++】c++中的map与hashmap

map & hashmap

c++里面也有map和hashmap,分别适用于不同场景。
详细介绍可以参考:

注意:虽然hashmap理论上查找在较好的情况下可以做到O(1),但是最好不要企图用hashmap取代数组,数组还是快与hashmap所以能用数组的直接用数组。
C++动态申请数组

	int n = 10;
	int* a = new int[n];
	/*
	* 其余代码
	*/
	delete[]a;

hashmap 示例代码
1.

#include<unordered_map> //由于编译器报错,把hash_map换为了unordered_map
#include<iostream>
using namespace std;

int main(void)
{
 unordered_map<unsigned int, unsigned int>hm;
 //元素的插入:pair<iterator,bool>insert<const value_type &v>
 //insert(inputiterator first,inputiterator last)
 hm[0]=4;
 hm[1]=5;
 hm[2]=6;
 hm[3]=7;
 //元素的访问可以用,数组的方式也可以用迭代器的方式
 unordered_map<unsigned int, unsigned int>::iterator i,iend,j;
 iend=hm.end();
 for(i=hm.begin();i!=iend;i++)
 {
  cout<<(*i).first<<"   "
   <<(*i).second<<endl;
 }
 cout<<"**************************************************"<<endl;
 //元素的删除erase(),clear()
 hm.erase(0);
 for(i=hm.begin();i!=iend;i++)
 {
  cout<<(*i).first<<"   "
   <<(*i).second<<endl;
 }
 
 //元素的搜索
 j=hm.find(1);
 i=hm.find(2);
 cout<<"水果:"<<(*i).first<<"  "
  <<"价钱:"<<(*i).second<<endl;
 cout<<"**************************************************"<<endl;
 if(j!=hm.end())
 {
  cout<<"hash_map容器的个数"<<hm.size()<<endl;
 }
 else
 {
  cout<<"哈希表的表长:"<<hm.bucket_count()<<endl;
 }
 return 0;
}
#include<unordered_map>
#include<iostream>
using namespace std;
unsigned int
int main(void)
{
 unordered_map<const char*,float>hm;
 //元素的插入:pair<iterator,bool>insert<const value_type &v>
 //insert(inputiterator first,inputiterator last)
 hm["apple"]=1.0f;
 hm["pear"]=1.5f;
 hm["orange"]=2.0f;
 hm["banana"]=1.8f;
 //元素的访问可以用,数组的方式也可以用迭代器的方式
 unordered_map<const char*,float>::iterator i,iend,j;
 iend=hm.end();
 for(i=hm.begin();i!=iend;i++)
 {
  cout<<(*i).first<<"   "
   <<(*i).second<<endl;
 }
 cout<<"**************************************************"<<endl;
 //元素的删除erase(),clear()
 hm.erase("pear");
 for(i=hm.begin();i!=iend;i++)
 {
  cout<<(*i).first<<"   "
   <<(*i).second<<endl;
 }
 
 //元素的搜索
 j=hm.find("pear");
 i=hm.find("apple");
 cout<<"水果:"<<(*i).first<<"  "
  <<"价钱:"<<(*i).second<<endl;
 cout<<"**************************************************"<<endl;
 if(j!=hm.end())
 {
  cout<<"hash_map容器的个数"<<hm.size()<<endl;
 }
 else
 {
  cout<<"哈希表的表长:"<<hm.bucket_count()<<endl;
 }
 return 0;
}

std::map默认值

对于非基本类型, 例如class/struct等通过构函数设置默认值,对于int类型可以使用-std=c++17,如下:

#include <iostream>
#include <vector>
#include <map>

int main()
{

   std::map<std::string, std::optional<int>> myNullables;
   std::cout << myNullables["empty-key"].value_or(-1) << std::endl;
    return 0;
}

/*
output:
-1
*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ystraw_ah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值