代码:
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> with1> [1> _Kty=std::string1> , _Ty=std::vector<int,std::allocator<int>>1> ]1> No constructor could take the source type, or constructor overload resolution was ambiguous1> 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 compiled1> with1> [1> _Ty=int1> , _Kty=std::string1> , T=std::string1> ]===================================================================================
解决方案当然是把iterator改成const_iterator
===================================================================================