-2

I want to add objects so a multi-map and some should have the same key how can I get access to all the elements which the same key?

Can I do a ranged fore lope over only the specific keys or something similar?

Isak True
  • 13
  • 2
  • 1
    https://en.cppreference.com/w/cpp/container/multimap -> Lookup -> https://en.cppreference.com/w/cpp/container/multimap/equal_range. Nobody knows all c++ by heart. You need to get used to look things up. I recommend cppreference. – 463035818_is_not_an_ai Jan 25 '23 at 10:50
  • @463035818_is_not_a_number sorry I just have h hard time understanding the cpprefrense simtimes – Isak True Jan 26 '23 at 09:11

1 Answers1

2

You can use the equal_range member, and wrap that in a ranges::subrange to range-for over it.

auto range = map.equal_range(key); 
for (auto & [_, value] : std::ranges::subrange(range.first, range.second)) {
    // ...
}

When ranged-for was being proposed, it was considered to let std::pair<iter, iter> be directly iterated over, but that was rejected as potentially dangerous.

See it on coliru

Caleth
  • 52,200
  • 2
  • 44
  • 75