为什么我不能将const map结构传递给c ++中的函数? [英] Why can't I pass const map structure to a function in c++?
本文介绍了为什么我不能将const map结构传递给c ++中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图传递const与向量它的工作原理:
Ex:
I tried to pass const with vector it works: Ex:
void damn(const vector <bool> &bb)
{
for (int i=0; i<bb.size(); i++)
cout<<bb[i]<<endl;
}
但是在使用地图时, p>
But when trying with map, it does not:
void pas(const map <string, float> &mm)
{
cout<<mm["a"];
cout<<mm["b"];
}
我不知道为什么不行。
推荐答案
我相信这是因为 []
在map中不是 const
,因为它创建具有默认值的新对,如果您寻址到不存在的对。尝试
I believe that it is because []
in map isn't const
, as it creates new pair with default value, if you address to nonexisting one. Try
void pas(const map <string, float> &mm)
{
cout<<mm.find("a")->second;
cout<<mm.find("b")->second;
}
这篇关于为什么我不能将const map结构传递给c ++中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文