在函数调用中使用vector <pair <int,int >> :: iterator&类型

时间:2019-02-15 01:45:20

标签: c++ compiler-errors std-function

我已经编写了以下测试代码。该代码段的功能是从给定的一组数字中找到最长的连续序列。我在与对std::function进行排序有关的处理逻辑部分中使用了递归lambda(vector<pair<int,int>>)实现。

#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

Vinod

3 个答案:

答案 0 :(得分:0)

猜猜,该错误与编译器输出中的第二行有关:

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值以消除错误。

答案 1 :(得分:0)

基于错误消息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);
...
}

答案 2 :(得分:0)

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()都是右值,并且不能绑定到您在函数签名中强加的非常量左值引用(vector<pair<int,int>>::iterator&)。