C 使用字符串项目在列表中查找结构
C++ Find struct in list using a string item?
我是C 的新手,我正在尝试弄清楚如何使用字符串在列表中找到结构。
我有这样的结构:
struct entrada {
string token;
string lexema;
string tipo;
};
和一个列表:
list<entrada> simbolos;
在这里插入一些" entrada"
假设我想搜索带有某种" lexema"的" entrada",然后搜索其他字符串。有什么简单的方法吗?像功能之类的东西。我是在/for时使用的,但这不是我想做的。
根据您的评论,以下片段向您显示了一种使用STL std::find_if
中的算法搜索元素到容器中的简单方法。
auto match = std::find_if(simbols.cbegin(), simbols.cend(), [] (const entrada& s) {
return s.lexema == "2";
});
if (match != simbols.cend()) {
std::cout << match->token << 'n'
<< match->lexema << 'n'
<< match->tipo << 'n';
}
实时演示
至少需要 C 11 。
相关文章:
- 如何在等效列表中查找最小的连接标签
- 在链接列表中查找元素 - C++
- 如何在随机数列表中查找大于或小于的元素
- C++使用旧编译器GCC 4.4.0在结构列表中查找字符串
- 使用特征查找两个列表之间的差异
- 在地图和列表之间查找标准容器
- 在由邻接列表表示的树中查找节点到另一个给定节点之间的路径
- 如何在数组或链接列表中查找特定字符串
- 如何查找结构列表项
- 查找可变参数列表的字符串格式指定符
- 穿过链接列表以查找字符串
- 通过查找一对项目的代码获取错误的输出,在给定的总和相等的给定列表中列表
- 在数据类型类的列表中查找 elem
- 迭代列表以查找元素的出现.代码问题
- 查找XOR列表的初始值
- 快速的唯一组合(来自重复项列表),无需查找
- 从列表到数组C 查找模式或最常见的数字
- 在字符串列表中查找字符串,然后将字符串设置为找到的字符串?
- 模板初始化列表-查找失败的原因
- 使用列表查找map中的元素