简体   繁体   中英

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>> .

#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

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.

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> *> &)>')

You could try to change the lambda func_obj2 to:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM