关于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;
}