我一直在寻找解决这个问题的办法,而且无法解决任何问题。我相信这是一个快速解决方案,希望有人可以发现我的错误并告诉我。
这就是我所拥有的
#include<algorithm>
#include<vector>
void myFunction ( cont std::vector<std::vector<int> > &counts){
std::vector<int>::iterator max_it;
int index;
for ( int i = 0 ; i < counts.size() ; i ++ ){
max_it = std::max_element(counts[i].begin(), counts[i].end());
index = std::distance(counts[i].begin(),max_it);
}
}
编译上面的代码时,对于调用max_element的行,我得到以下错误。
error: no match for ‘operator=’ in ‘it = std::max_element [with _ForwardIterator = __gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >](((const std::vector<int, std::allocator<int> >*)((const std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >*)counts)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >](((long unsigned int)i)))->std::vector<_Tp, _Alloc>::begin [with _Tp = int, _Alloc = std::allocator<int>](), ((const std::vector<int, std::allocator<int> >*)((const std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >*)counts)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >](((long unsigned int)i)))->std::vector<_Tp, _Alloc>::end [with _Tp = int, _Alloc = std::allocator<int>]())’
/ usr / include / c ++ / 4.2.1 / bits / stl_iterator.h:637:注意:候选人是:__ gn_cxx :: __ normal_iterator&gt; &GT;&安培; __gnu_cxx :: __ normal_iterator&gt; &gt; :: operator =(const __gnu_cxx :: __ normal_iterator&gt;&gt;&amp;)
我尝试了几种解决方案,主要涉及更改我声明max_it迭代器的方式。到目前为止没有任何工作,我无法破译错误消息。
答案 0 :(得分:7)
你的迭代器应该是一个常量迭代器,因为你通过引用begin()
来调用end()
和const
:
std::vector<int>::const_iterator max_it;
// ^^^^^^