我写了下面的测试代码。该代码段的功能是从给定的一组数字中找到最长的连续序列。我在处理逻辑的一部分中使用递归lambda(std::function
)实现与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
维诺德
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左值引用(vector<pair<int,int>>::iterator&
)。
猜猜,错误与编译器输出中的第二行有关:
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);
...
}