使用STL map和模板时遇到的一个错误

代码:

template<typename T, typename U>
bool MapContainsKey(const map<T,U>& m,T key)
{
for (map<T, U>::iterator it = m.begin(); it != m.end(); ++it)//差点玩死我
{
if (it->first == key)
{
return true;
}
}
return false;
}

报错如下:=======================================================================

1>e:\program files\project\myregex20150310\test\source.cpp(38):
error C2440: 'initializing' : cannot convert from
'std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>'
to
'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>'
1> with
1> [
1> _Kty=std::string
1> , _Ty=std::vector<int,std::allocator<int>>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1> e:\program files\project\myregex20150310\test\source.cpp(70) : see reference to function template instantiation
'bool MapContainsKey<std::string,std::vector<int,std::allocator<_Ty>>>
(const std::map<std::string,std::vector<_Ty,std::allocator<_Ty>>,
std::less<_Kty>,
std::allocator<std::pair<const _Kty,std::vector<_Ty,std::allocator<_Ty>>>>> &,
T)'
being compiled
1> with
1> [
1> _Ty=int
1> , _Kty=std::string
1> , T=std::string
1> ]

===================================================================================

解决方案当然是把iterator改成const_iterator

===================================================================================

转载于:https://www.cnblogs.com/shenwudi/p/4331357.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值