簡體   English   中英

如何在二維向量上使用std :: max_element

[英]How to use std::max_element on a two dimensional vector

我到處尋找解決此問題的方法,無法打開任何東西。 我確信這是一個快速修復,希望有人可以發現我的錯誤並告訴我。

這就是我所擁有的

#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迭代器的方式。 到目前為止沒有任何工作,我無法破譯錯誤消息。

您的迭代器應該是一個常量迭代器,因為您是通過引用const來調用begin()end()

std::vector<int>::const_iterator max_it;
//                ^^^^^^

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM