C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用映射C中的值获取匹配键大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个结构作为值类型的地图 @H_524_2@map<int id,struct_t*> table struct_t { int prev; int wt; String name; }

仅使用prev,我需要找到相应的id.非常感谢提前!

编辑:

int key=0;
for(auto it = table.begin(); it != table.end(); ++it)
{
     if(table[(*it).first].prev == ?)
}

这就是我的地图数据的样子:

id    prev abundance  thing
1573  -1      0       book
1864  1573    39      beds
2075  1864    41      tray
1760  2075    46      cups

对于每个id,我需要找到NEXT匹配id.因此,对于来自prev列的1573,我需要找到匹配的’id’,即1864.此外,std :: next不起作用,因为数据集可以具有匹配的id,不一定在下一个元素中.希望这有帮助!

请帮帮我!!!我的老板已经很失望,因为我花了这么多时间学习C(已经有3周了!)

解决方法

如果您有一个现代编译器(支持lambdas),您可以执行以下操作:
const int prevToFind = 10;
auto findResult = std::find_if(std::begin(tablE),std::end(tablE),[&](const std::pair<int,struct_t*> &pair)
{
    return pair.second->prev == prevToFind;
});

int foundKey = 0; // You might want to initialise this to a value you kNow is invalid in your map
struct_t *foundValue = nullptr
if (findResult != std::end(tablE))
{
    foundKey = findResult->first;
    foundValue = findResult->second;

    // Now do something with the key or value!
}

如果您有一个较旧的编译器,请告诉我,我可以更新示例以使用谓词类.

大佬总结

以上是大佬教程为你收集整理的如何使用映射C中的值获取匹配键全部内容,希望文章能够帮你解决如何使用映射C中的值获取匹配键所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。