c – STL迭代器std :: distance()错误

前端之家收集整理的这篇文章主要介绍了c – STL迭代器std :: distance()错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个typedef: @H_301_2@typedef std::vector<int> Container; typedef std::vector<int>::const_iterator Iter; @H_502_4@在我考虑的问题中,我对Container Input执行了一些操作,之后我想计算std :: distance(Input.begin(),itTarget),其中itTarget属于Iter类型.但是我得到这个编译错误,没有函数模板“std :: distance”的实例匹配参数列表,并且只有在转换后,即std :: distance(static_cast< Iter>(Input.begin()),itTarget一切正常.

@H_502_4@我想知道为什么会这样?

解决方法

std::distance是模板函数,它不能接受不同的参数.
你需要使用: @H_301_2@std::distance(Input.cbegin(),itTarget); ^^ @H_502_4@见std::vector::cbegin链接

猜你在找的C&C++相关文章