为什么std :: distance不工作在const和nonconst迭代器的混合? [英] Why std::distance doesnt work on mix of const and nonconst iterators?
本文介绍了为什么std :: distance不工作在const和nonconst迭代器的混合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
像问题所说,我想知道原因。因为当我尝试获取 const
和非 - const
迭代器之间的距离时,我得到一个错误。
Like the question says, I'm wondering about the reason for that. Because I get an error when I try to get the distance between const
and non-const
iterators.
vector<int> v;
auto it=v.begin();
auto cit=v.cbegin();
distance(it,cit);
no matching function for call to ‘distance(__gnu_cxx::__normal_iterator<int*, std::vector<int> >&, __gnu_cxx::__normal_iterator<const int*, std::vector<int> >&)
从我对迭代器的有限理解,没有理由为什么它不应该工作。
From my limited understanding of iterators, I see no reason why it shouldn't work.
推荐答案
在调用 std :: distance
You have a mutable iterator and a constant iterator in the call to std::distance
, so template argument deduction is failing. You can fix this by specifying the template argument explicitly.
std::vector<int> v;
auto i1 = v.begin();
auto i2 = v.cbegin();
auto dist = std::distance<std::vector<int>::const_iterator>( i1, i2 );
这篇关于为什么std :: distance不工作在const和nonconst迭代器的混合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文