[英]I am trying to find a specific struct member which a vector of structs using a lambda function
I have been given the following struct.我得到了以下结构。 I have a vector of tps.我有一个 tps 向量。
struct tp{
unsigned int channel;
unsigned int tstart;
unsigned int tspan;
unsigned int adcsum;
unsigned int adcpeak;
unsigned int flags;
};
I cannot modify the struct nor add an operator to it.我无法修改结构,也无法向其添加运算符。 I have a vector(all_candidates) of specific tstarts that i need to look for on the vector of tps.我有一个特定 tstart 的向量(all_candidates),我需要在 tps 的向量上寻找它。
std::vector< TP> Tps; //these are the input TPs.
std::vector< std::pair<double,double> > all_candidates;//every element is a time-tstart from a TP.
const auto& tmp1 = &(all_candidates.at[0].first);
auto first_tp = std::find_if(candidates.begin(),candidates.end(),[&tmp1](const TP& tp_1){return tp_1.first_time == tmp1 ;});
But when i run this code I get a compiling error, the first one being:但是当我运行这段代码时,我得到一个编译错误,第一个是:
error: reference to non-static member function must be called
const auto& tmp1 = all_candidates.at[0].first;
what is the correct syntax to find the TPs given a start?给定开始时找到 TP 的正确语法是什么? Thanks谢谢
As the error tells you, the line正如错误告诉你的那样,这条线
const auto& tmp1 = &(all_candidates.at[0].first);
is the problem.是问题所在。 As Algirdas Preidžius noted, it should be all_candidates.at(0)
.正如 Algirdas Preidžius 所说,它应该是all_candidates.at(0)
。 Also, the second &
is superfluous and is interpreted as an address operator.此外,第二个&
是多余的,被解释为地址运算符。 The line should be:该行应该是:
const auto& tmp1 = all_candidates.at(0).first;
However, since tstart
is a simple int
, there is no need for references here.但是,由于tstart
是一个简单的int
,因此这里不需要引用。 You could simple write:你可以简单地写:
const int tmp1 = all_candidates.at(0).first;
Likewise, you can just capture tmp1
by value instead of by reference in your lambda.同样,您可以在 lambda 中按值而不是按引用捕获tmp1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.