【发布时间】:2016-08-18 11:45:28
【问题描述】:
我有一个结构如下:
struct deviceDescription_t
{
std::string deviceID;
std::string deviceDescription;
};
我已经定义了一个向量如下:
std::vector<deviceDescription_t> deviceList
假设向量deviceList 由以下元素组成:
ID Description
=================
one_1 Device 1
two_2 Device 2
three_3 Device 3
....
我需要搜索 deviceList 中的 ID 字段并获取相关说明。假设我有 one 作为谓词(搜索字符串)。我现在必须查看 deviceList 中的 ID 字段以找到我正在使用的匹配项
std::string temp = deviceID.substr(0, deviceID.find("_"));
但我不确定如何使用this 问题中提到的find_if。
作为一个答案,建议使用
auto iter = std::find_if(deviceList.begin(), deviceList.end(),
[&](deviceDescription_t const & item) {return item.deviceID == temp;});
在我的函数中使用上面的,抛出以下错误
托管类的成员函数中不允许本地类、结构或联合定义。
谁能指导我如何使用 find_if 找到匹配搜索条件的元素并返回描述?
【问题讨论】:
-
你在使用 C++/CLI 吗?
-
请尝试创建一个Minimal, Complete, and Verifiable Example 并向我们展示。由于你表现得很少,不可能肯定地说什么。
-
本机编译正常。