Phi*_*eme 1 c++ iterator std max
我四处寻找解决这个问题的办法,却无法发现任何问题.我相信这是一个快速解决方案,希望有人可以发现我的错误并告诉我.
这就是我所拥有的
#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);
}
}
Run Code Online (Sandbox Code Playgroud)
在编译上面的代码时,我得到了调用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>]())’
Run Code Online (Sandbox Code Playgroud)
/usr/include/c++/4.2.1/bits/stl_iterator.h:637:注意:候选人是:__ gn_cxx :: __ normal_iterator >>&__gnu_cxx :: __ normal_iterator >> :::运营商=(const __gnu_cxx :: __ normal_iterator >> && )
我尝试了几种解决方案,主要涉及更改我声明max_it迭代器的方式.到目前为止没有任何工作,我无法破译错误消息.
你的迭代器应该是一个常量迭代器,因为你正在调用begin()
并end()
通过引用const
:
std::vector<int>::const_iterator max_it;
// ^^^^^^
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
465 次 |
最近记录: |