gpt4 book ai didi

c++ - 在 C++ 中使用类似 lambda 的语法搜索结构列表

转载 作者:行者123 更新时间:2023-11-27 23:26:21 24 4
gpt4 key购买 nike

struct Data
{
int Id;
std::vector<int> SomeData
}

std::vector<Data> dataList = ....

int wantedIndex = 11;
std::find(dataList.begin(), dataList.end(), "where dataList.Id == wantedIndex")

在 Visual Studio 2008 中,这种语法是否可以在标准 C++ 中以某种方式实现?

最佳答案

如果你不能使用 C++11。也许你可以使用 boost phoenix .

应该是这样的:

#include <boost\phoenix\phoenix.hpp>
...
std::find_if(dataList.begin(), dataList.end(),
boost::phoenix::bind(&Data::Id, boost::phoenix::placeholders::arg1) == boost::phoenix::val(wantedIndex)));

虽然我认为在您非常简单的情况下,从 boost 绑定(bind)也可以。

std::find_if(dataList.begin(), dataList.end(), boost::bind(&Data::Id, _1) == wantedIndex);

关于c++ - 在 C++ 中使用类似 lambda 的语法搜索结构列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9211235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com