348_C++_find_if应用,查找过程是通过一个lambda表达式完成,查找容器中参数和[传入参数]有无相等参数

addpair添加一系列std::pair<std::string, int>到list中


bool ApiPairsNodeV2::AddPair(const std::string &pairName, bool usePairValue, const int pairValue)
{
   
    if (_bIntPairsName)
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
名表(也称字典)是一种用于存储键值对的数据结构,其每个键都唯一对应一个值。在C++,可以使用STL的map容器来实现名表表示和查找功能,同时使用find_if算法来查找指定的键值。 以下是一个使用map容器和find_if算法实现名表表示和查找功能的示例代码: ```c++ #include <iostream> #include <map> #include <algorithm> #include <string> using namespace std; int main() { // 创建一个名表,其键是字符串类型,值是整数类型 map<string, int> name_table; // 向名表添加一些键值对 name_table["Tom"] = 18; name_table["Alice"] = 20; name_table["Bob"] = 22; // 查找名字为Alice的年龄 string name = "Alice"; auto it = find_if(name_table.begin(), name_table.end(), [&name](const pair<string, int>& p) { return p.first == name; }); if (it != name_table.end()) { cout << "The age of " << name << " is " << it->second << endl; } else { cout << "No record found for " << name << endl; } // 查找名字为David的年龄 name = "David"; it = find_if(name_table.begin(), name_table.end(), [&name](const pair<string, int>& p) { return p.first == name; }); if (it != name_table.end()) { cout << "The age of " << name << " is " << it->second << endl; } else { cout << "No record found for " << name << endl; } return 0; } ``` 输出结果为: ``` The age of Alice is 20 No record found for David ``` 在上面的代码,我们首先创建了一个名表name_table,并向其添加了三个键值对。然后,我们使用find_if算法查找名字为Alice和David的年龄,并输出结果。在查找时,我们使用lambda表达式来实现指定键值的匹配条件,即判断键是否等于指定的名字。如果找到了匹配的键值对,则输出对应的值;否则输出未找到记录的提示信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扳手的海角

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值