简体   繁体   English

使用类型向量 <pair<int,int> &gt; :: iterator&在函数调用中

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

I have written the following piece of test code. 我已经编写了以下测试代码。 The functionality of this code segment is to find the longest continuous sequence from a given set of digits. 该代码段的功能是从给定的一组数字中找到最长的连续序列。 I am using a recursive lambda ( std::function ) implementation in the part of the processing logic related to sorting the vector<pair<int,int>> . 我在与对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;
}

The code fails to compile: 该代码无法编译:

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.

Can someone advise about how to rectify this? 有人可以建议如何纠正此问题吗?

TIA TIA

Vinod 维诺德

Guess, the error was related to the second line in the compiler output: 猜猜,该错误与编译器输出中的第二行有关:

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.

Changed the invocation arguments to l-values to get rid of the error. 将调用参数更改为l值以消除错误。

based on the error message 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> *> &)>') 基于错误消息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> *> &)>')

You could try to change the lambda func_obj2 to: 您可以尝试将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 = /*...*/;

should be 应该

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

Or even should simply be: 甚至应该只是:

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

Issue is that in func_obj3(r.begin(), r.end()) , r.begin() and r.end() are both rvalues and cannot bind to non-const lvalue references ( vector<pair<int,int>>::iterator& ) that you impose in function signature. 问题是在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