首页 > 解决方案 > 我试图找到一个特定的结构成员,它是一个使用 lambda 函数的结构向量

问题描述

我得到了以下结构。我有一个 tps 向量。

struct tp{
  unsigned int channel;
  unsigned int tstart;
  unsigned int tspan;
  unsigned int adcsum;
  unsigned int adcpeak;
  unsigned int flags;
};

我无法修改结构,也无法向其添加运算符。我有一个特定 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 ;});

但是当我运行这段代码时,我得到一个编译错误,第一个是:

 error: reference to non-static member function must be called
    const auto& tmp1 = all_candidates.at[0].first; 

给定开始时找到 TP 的正确语法是什么?谢谢

标签: c++vectorstructlambdacompilation

解决方案


正如错误告诉你的那样,这条线

const auto& tmp1 = &(all_candidates.at[0].first);

是问题所在。正如 Algirdas Preidžius 所说,它应该是all_candidates.at(0). 此外,第二个&是多余的,被解释为地址运算符。该行应该是:

const auto& tmp1 = all_candidates.at(0).first;

不过,既然tstart是简单的int,这里就不需要参考了。你可以简单地写:

const int tmp1 = all_candidates.at(0).first;

同样,您可以tmp1在 lambda 中按值而不是按引用进行捕获。


推荐阅读