簡體   English   中英

使用類型向量 <pair<int,int> &gt; :: iterator&在函數調用中

[英]Using type vector<pair<int,int>>::iterator& in function call

我已經編寫了以下測試代碼。 該代碼段的功能是從給定的一組數字中找到最長的連續序列。 我在與對vector<pair<int,int>>進行排序有關的處理邏輯部分中使用了遞歸lambda( std::function )實現。

#include <iostream>
#include <vector>
#include <functional>
#include <utility>
#include <string>
using std::vector;
using std::function;
using std::swap;
using std::pair;
using std::make_pair;
using std::string;
using std::cout;
using std::endl;

int main (void){

auto func_obj1 = [&](const string& r, int buffer)->int{

  function<void(vector<pair<int,int>>::iterator&, vector<pair<int,int>>::iterator&)> func_obj3 = [&](auto begin, auto end){
    if(begin == end){
      return;
    }
    auto tempb = begin;
    auto tempe = end - 1;
    while(tempb != end){
      if((*tempb).second > (*tempe).second){
        swap(*tempb, *tempe);
      }
    }
    end = tempe;

    func_obj3(begin, end);
  };
  auto func_obj2 = [&](vector<pair<int,int>>& r)->int{
    func_obj3(r.begin(), r.end());

    auto iter = r.begin();
    auto count = 0;
    auto max_count = 0;

    while(iter != r.end()){
      auto temp = iter + 1;
      if((*iter).second + 1 == (*temp).second){
        ++count;
        if(count > max_count){
          max_count = count;
        }
      }
      else{
        count = 0;
      }
      ++iter;
    }
    return max_count;
  };

  vector<pair<int,int>> v;
  auto sws = ' ';
  auto iter = r.begin();
  auto count = 0;

  while(iter != r.end()){
    if(*iter == sws){
      continue;
    }
    else{
      v.push_back(make_pair(count, *iter));
      ++count;
    }
    ++iter;
  }
  auto result = func_obj2(v);

  return result;
};

string s = "1 9 2 7 3 8 4 ";

auto result = func_obj1(s, s.size());

cout << result << endl;

  return 0;
}

該代碼無法編譯:

g++ -ggdb -std=c++14 -Wall code.cpp

code.cpp:35:5: error: no matching function for call to object of type 'function<void (vector<pair<int, int> >::iterator &, vector<pair<int, int> >::iterator &)>' (aka 'function<void (__wrap_iter<std::__1::pair<int, int> *> &, __wrap_iter<std::__1::pair<int, int> *> &)>')
    func_obj3(r.begin(), r.end());
    ^~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/functional:1677:9: note: candidate function not viable: expects an l-value for 1st argument
    _Rp operator()(_ArgTypes...) const;
        ^
1 error generated.

有人可以建議如何糾正此問題嗎?

TIA

維諾德

猜猜,該錯誤與編譯器輸出中的第二行有關:

code.cpp:39:5: error: no matching function for call to object of type 'function<void (vector<pair<int, int> >::iterator &, vector<pair<int, int> >::iterator &)>' (aka 'function<void (__wrap_iter<std::__1::pair<int, int> *> &, __wrap_iter<std::__1::pair<int, int> *> &)>')
    func_obj3(r.begin(), r.end());
    ^~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/functional:1677:9: note: **candidate function not viable: expects an l-value for 1st argument**
    _Rp operator()(_ArgTypes...) const;
        ^
1 error generated.

將調用參數更改為l值以消除錯誤。

基於錯誤消息code.cpp:35:5: error: no matching function for call to object of type 'function<void (vector<pair<int, int> >::iterator &, vector<pair<int, int> >::iterator &)>' (aka 'function<void (__wrap_iter<std::__1::pair<int, int> *> &, __wrap_iter<std::__1::pair<int, int> *> &)>')

您可以嘗試將lambda func_obj2更改為:

auto func_obj2 = [&](vector<pair<int,int>>& r)->int{
    using vpitor = std::vector<pair<int,int>>::iterator;
    vpitor it_begin = r.begin();
    vpitor it_end = r.end();
    func_obj3(it_begin, it_end);
...
}
function<void(vector<pair<int,int>>::iterator&, vector<pair<int,int>>::iterator&)>
    func_obj3 = /*...*/;

應該

function<void(vector<pair<int,int>>::iterator, vector<pair<int,int>>::iterator&)>
    func_obj3 = /*...*/;

甚至應該只是:

auto func_obj3 = [&](auto begin, auto end){/*...*/};

問題是在func_obj3(r.begin(), r.end())r.begin()r.end()都是rvalues並且不能綁定到非const lvalue引用( vector<pair<int,int>>::iterator& ),您可以在函數簽名中加入。

暫無
暫無

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

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