C++ c++;如何将集合的迭代器映射作为键
我有这张地图:C++ c++;如何将集合的迭代器映射作为键,c++,C++,我有这张地图: std::map<std::set<int>, float> myMap; 我想比较这些集合,例如,如果集合A(i1,i2,…in-1)等于集合B(j1,j2,…jn-1),使得i1=j1,i2=j2,…in-1=jn-1,那么: 我将创建新的集合C(i1,i2,…in,jn) 因此在第一次迭代之后,我将有以下集合: (7,9,11),(7,9,13),(7,9,16),(7,9,17),(7,11,13),(7,11,16),(7,11,17),(7,
std::map<std::set<int>, float> myMap;
我想比较这些集合,例如,如果集合A(i1,i2,…in-1)等于集合B(j1,j2,…jn-1),使得i1=j1,i2=j2,…in-1=jn-1,那么:
我将创建新的集合C(i1,i2,…in,jn)
因此在第一次迭代之后,我将有以下集合:
(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)
在第二次迭代之后,我将得到以下集合:
(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)**停止,不再设定**
第三次迭代后,我将得到以下集合:
(7,9,11,13,16),(7,9,11,13,17),(7,9,13,16,17),(7,9,13,16,17)**停止,不再设定**
这是代码
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;
}
}
for(const auto&a:myMap)
{
对于(自动b=++myMap.begin();b!=myMap.end();++b)
{
bool equal=std::equal(a.first.begin(),--(a.first.end()),b->first.begin();//t感谢@povilasb
如果(相等)
{
std::set_union(a.first.begin(),a.first.end(),b->first.begin(),b->first.end(),std::inserter(dest1,dest1.begin());
for(set::iterator it=dest1.begin();it!=dest1.end();it++)
cout似乎a
应该保持其在嵌套循环中的位置:
for (auto a = myMap.begin(); a != myMap.end(); ++a)
{
auto b = a;
for (++b; b != myMap.end(); ++b)
{
// Replace "a." with "a->"
...
}
}
什么错误?什么是FrequentList
?此操作中使用的map
的值是什么?如果不是,您是否尝试过在集合中收集map
的键,并在递归函数中处理该集合
?谢谢,它适用于myMap
中的所有对,我只是注意到我需要这样做编辑我的代码,使其在一般情况下工作(上面示例中的第二次、第三次……等迭代)
for (auto a = myMap.begin(); a != myMap.end(); ++a)
{
auto b = a;
for (++b; b != myMap.end(); ++b)
{
// Replace "a." with "a->"
...
}
}