我有一个具有结构
的多图multimap< pair<int,int>, bool >
我已插入数据,看起来像这样
I is [0] Int is [5] Bool is [0]
I is [0] Int is [100] Bool is [0]
I is [0] Int is [100] Bool is [0]
I is [1] Int is [100] Bool is [0]
I is [1] Int is [100] Bool is [0]
I is [1] Int is [100] Bool is [0]
I is [2] Int is [5] Bool is [0]
I is [2] Int is [100] Bool is [0]
I is [2] Int is [100] Bool is [0]
我需要能够让地图的迭代器跳转到下一个我,而不会迭代其他我。
例如,我只想添加不具有相同 I 值的 Ints 。所以它可以去
添加5 + 100 + 5
因为这些是第一个具有不同我的值。我怎么能这样做呢?
答案 0 :(得分:0)
嗯 - 您可以使用地图lower_bound
功能 - 您需要将其应用于“下一个”主键 - 请参阅:
auto next_key(int key_first)
{
return std::make_pair(key_first + 1, std::numeric_limits<int>::min());
}
因此,下一个要搜索的密钥对是(first + 1, INT_MIN)
。
所以,循环:
for (auto i = data.begin();
i != data.end();
i = data.lower_bound(next_key(i->first.first)))
{
std::cout << i->first.second << std::endl;
}
对于这些数据:
std::multimap< std::pair<int,int>, bool > data = {
{{0,5},false},
{{0,100},false},
{{0,100},false},
{{1,100},false},
{{1,100},false},
{{1,100},false},
{{2,5},false},
{{2,100},false},
{{2,100},false}
};
你得到:
5
100
5