c++ - 将映射中的键与函数进行比较

标签 c++ dictionary

我有一个 map<int, string> . key 指的是客户端节点。

我需要遍历 map ,并将每个键与 map 中保存的每个其他键与 bool 函数(检查节点是否连接)进行比较。

即做类似的事情的最佳方法是什么

map<int, string> test_map;
map<int, string>::iterator iter;

for (iter = test_map.begin(); iter!=test_map.end(); iter++)
{
    int curr_node = iter->first;

    /* psuedo-code:
    1. iterate through other keys
    2. check against boolean e.g. bool fn1(curr_node, test_node) returns true if nodes are connected
    3. perform fn2 if true */

}

我不确定如何使用节点中的其他键进行迭代部分 - 非常感谢。

最佳答案

完全天真的解决方案是这样的:

map<int, string>::iterator iter, iter2;

for ( iter = test_map.begin(); iter != test_map.end(); iter++)
{
    int curr_node = iter->first;
    for ( iter2 = test_map.begin(); iter2 != test_map.end(); iter2++)
    {
        if( iter == iter2 ) continue;
        int test_node = iter2->first;
        if( fn1(curr_node, test_node) ) fn2();
    }
}

关于c++ - 将映射中的键与函数进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16952987/

相关文章:

c++ - 抛出异常 : write access violation. _My_data 为 0x7001AC

c++ - 是否显示Common Item Dialog 或GetOpenFileName? (Win32 API)

string - 使用 Scala 按顺序查找两个字符串的交集

java - 检索与字符串同名的变量

c++ - 将应用程序的选项添加到 Windows 资源管理器上下文菜单

c++ - 如何从派生类访问派生基成员?(在 C++ 中)

c++ - 我不明白代码的某些部分

c# - 在异步上下文中添加到字典时出现 NullReferenceException

Swift二维字典错误

Scala 映射和/或 groupby 函数