map的value默认值问题

关于map的value的默认值问题
结论:

bool           ----默认值为------->      false;
int/double...  ----默认值为------->      0;
string         ----默认值为------->      "";
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
unordered_map<int,bool> mp_bool;
unordered_map<int,int> mp_int;
unordered_map<int,string> mp_string;
unordered_map<int,double> mp_double;
int main(){
	if(mp_bool[1]==false) cout<<"false";
	else cout<<"True";
	cout<<endl<<"int的默认值:"<<mp_int[1]<<endl;
	if(mp_string[1]=="") cout<<"NULL"<<endl;
	else
		cout<<"string的默认值:"<<mp_string[1]<<endl;
	cout<<"double的默认值:"<<mp_double[1]<<endl;
} 

大家看看输出
在这里插入图片描述

下面再看看struct的默认值问题。其默认值就是结构体的内部元素类型的默认值,和上面一样的。

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
struct Node{
	int num;
	string s;
}; 
unordered_map<int,bool> mp_bool;
unordered_map<int,int> mp_int;
unordered_map<int,string> mp_string;
unordered_map<int,double> mp_double;
unordered_map<int,Node> mp_struct;
int main(){
//	if(mp_bool[1]==false) cout<<"false";
//	else cout<<"True";
//	cout<<endl<<"int的默认值:"<<mp_int[1]<<endl;
//	if(mp_string[1]=="") cout<<"NULL"<<endl;
//	else
//		cout<<"string的默认值:"<<mp_string[1]<<endl;
//	cout<<"double的默认值:"<<mp_double[1]<<endl;
	if(mp_struct.find(1)==mp_struct.end()) cout<<"未找到该元素"<<endl; 	
	cout<<mp_struct[1].s<<mp_struct[1].num<<endl;
	Node a = {1,"haha"};
	mp_struct[1] = a;
	cout<<mp_struct[1].s<<mp_struct[1].num;
}

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值