在函数调用中使用类型 vector<pair<int,int>>::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.
将调用参数更改为左值以消除错误。
根据错误信息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()
都是右值,不能绑定到您在函数中强加的非常量左值引用 (vector<pair<int,int>>::iterator&
)签名。
我写了下面一段测试代码。此代码段的功能是从给定的一组数字中找到最长的连续序列。我在与 vector<pair<int,int>>
.
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.
将调用参数更改为左值以消除错误。
根据错误信息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()
都是右值,不能绑定到您在函数中强加的非常量左值引用 (vector<pair<int,int>>::iterator&
)签名。