1

我四处寻找解决此问题的方法,但无法找到任何东西。我相信这是一个快速修复,希望有人能发现我的错误并告诉我。

这是我所拥有的

#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:注意:候选人是:__gnu_cxx::__normal_iterator > >& __gnu_cxx::__normal_iterator > >::operator=(const __gnu_cxx::__normal_iterator > >& )

我尝试了几种解决方案,主要涉及更改我声明 max_it 迭代器的方式。到目前为止没有任何效果,我无法破译错误消息。

4

1 回答 1

7

您的迭代器应该是一个常量迭代器,因为您正在调用begin()end()通过引用const

std::vector<int>::const_iterator max_it;
//                ^^^^^^
于 2013-06-04T19:08:46.497 回答