简体   繁体   English

如何将集合的映射作为键迭代

[英]c++ how can I iterator map of sets as key

I have this map: 我有这张地图:

std::map<std::set<int>, float> myMap;

and myMap is initialized as: 并且myMap初始化为:

{ (7, 9), 0.63 } 
{ (7, 11), 0.66 } 
{ (7, 13), 0.72 }
{ (7, 16), 0.73 }
{ (7, 17), 0.67 } 
{ (9, 13), 0.63 } 
{ (9, 16), 0.65 }
{ (9, 18), 0.61 } 

i want to compare these set such as, if the set A(i1,i2,...in-1) equal to set B(j1,j2,...jn-1) such that i1=j1, i2=j2,.....in-1=jn-1, then: 我想比较这些集合,例如,如果集合A(i1,i2,... in-1)等于集合B(j1,j2,... jn-1)使得i1 = j1,i2 = j2 ,..... in-1 = jn-1,然后:

I will create new set C(i1,i2,.....in,jn) 我将创建新的集合C(i1,i2,..... in,jn)

so after the first iteration i will have these sets: 因此,在第一次迭代之后,我将拥有以下集合:

(7,9,11), (7,9,13), (7,9,16), (7,9,17), (7,11,13), (7,11,16), (7,11,17), (7,13,16),(7, 13,17), (7,16,17) (7,9,11),(7,9,13),(7,9,16),(7,9,17),(7,11,13),(7,11,16),(7 ,11,17),(7,13,16),(7,13,17),(7,16,17)

(9,13,16), (9,13,18), (9,16,18) (9,13,16),(9,13,18),(9,16,18)

and after the second iteration i will have these sets: 在第二次迭代后,我将得到以下集合:

(7,9,11,13), (7,9,11,16), (7,9,11,17), (7,9,13,16), (7,9,13,17), (7,9,16, 17) (7,9,11,13),(7,9,11,16),(7,9,11,17),(7,9,13,16),(7,9,13,17), (7,9,16,17)

(9,13,16,18) ** Stop, no more set!!** (9,13,16,18)**停止,不再设置!**

and after the third iteration i will have these sets: (7,9,11,13,16), (7,9,11,13,17), (7,9,13,16,17), (7,9,13,16,17) ** Stop, no more set!!** 在第三次迭代之后,我将具有以下集合: (7,9,11,13,16),(7,9,11,13,17),(7,9,13,16,17),(7, 9,13,16,17)**停止,不再设置!! **

this is the code 这是代码

for (const auto& a : myMap)
    {   

        for (auto  b = ++myMap.begin(); b != myMap.end(); ++b)
        {
            bool equal = std::equal(a.first.begin(), --(a.first.end()), b->first.begin()); // tthanks to @povilasb

            if (equal)
            {
                std::set_union(a.first.begin(), a.first.end(), b->first.begin(), b->first.end(), std::inserter(dest1, dest1.begin()));

                for (set<int> ::iterator it = dest1.begin(); it != dest1.end(); it++)
                    cout << *it << " ";
                cout << endl;
                dest1.clear();
            }
            else // since `myMap` is already sorted, no need to continue comparing if 'equal` is false, so i exit the internal loop
                break;

        }

        }

I want to edit the internal loop for (auto b = ++myMap.begin(); b != myMap.end(); ++b) so b stats always from the next set after a 我想编辑的内部循环 for (auto b = ++myMap.begin(); b != myMap.end(); ++b)因此b从下一集总统计后a

I tried for (auto b = ++a.first.begin(); b != myMap.end(); ++b) but i got error in b != myMap.end(); 我尝试for (auto b = ++a.first.begin(); b != myMap.end(); ++b)但是我在b != myMap.end();出错

It seems that a should keep its position in the nested loop: 似乎a应该保留其在嵌套循环中的位置:

for (auto a = myMap.begin(); a != myMap.end(); ++a)
{
    auto b = a;
    for (++b; b != myMap.end(); ++b)
    {
        // Replace "a." with "a->"
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM