c++如何将迭代器映射的集合作为键
c++ how can I iterator map of sets as key
我有这个地图:
std::map<std::set<int>, float> myMap;
和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 }
我想比较这些集合,如,如果集合A(i1,i2,…in-1)等于集合B(j1,j2,…jn-1)使得i1=j1, i2=j2,.....1 = jn-1,那么:
创建集C(i1,i2,.....in,jn)
so 在第一次迭代后,我将有这些集合:
(7、9、11),(7、9、13),(7、9、16)、(17)7日,9日,(7、11、13),(7、11、16),(7、11、17),(7、13、16)、(17)7,13日,(7、16、17)
(9,13,16), (9,13,18), (9,16,18)
和在第二次迭代后,我将有这些集合:
(7、9、11、13),(7 9 11、16)、(17)7日,9日,11日,(7、9、13、16),(7、9、13、17),(7、9、16、17)
(9,13,16,18) **停止,不再设置!* *
和在第三次迭代之后,我将得到这些集合:(7、9、11、13、16)、(17)7,9,11,13日,(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 (auto b = ++myMap.begin(); b != myMap.end(); ++b)
所以b
的状态总是从a
之后的下一组开始
我试了for (auto b = ++a.first.begin(); b != myMap.end(); ++b)
但我得到错误的b != myMap.end();
看来a
应该保持它在嵌套循环中的位置:
for (auto a = myMap.begin(); a != myMap.end(); ++a)
{
auto b = a;
for (++b; b != myMap.end(); ++b)
{
// Replace "a." with "a->"
...
}
}
相关文章:
- 打印无序映射的第二个元素,即集合
- C++ 在集合和映射容器中获取唯一值
- C++ 中集合和映射中值的lower_bound索引
- 如何获取二叉树(集合或映射)的根
- 使用集合和映射来解决模式问题
- 如何在C++中迭代集合映射(std::map<std::set< char>, int >)?
- Qt:为模板(映射、列表、集合等)构建一个可变迭代器
- 通过std ::变换使std ::映射到集合
- 表达式:映射/集合迭代器不可递增
- 映射/集合迭代器不兼容-检查对象是否在映射中
- 映射/集合迭代器不兼容-检查映射中是否存在键
- 如何在 std::map 中使用 emplace(),其值为 std::set(从某物映射到集合)
- 如何命名同时充当其他集合容器的B+树键/值映射集合
- 将集合/获取请求映射到类/结构更改C++
- 标准::映射 标准::集合包含重复的键
- 如何在定义映射/集合时实例化比较函数(函子)
- std::插入无序集合(或映射)的迭代器
- 使用集合作为映射键的机制
- 我希望迭代器从映射或集合中擦除元素不会比对数差
- 在std集合/映射中使用double作为键的方法