Experiments with C++ PBDS: Failed writing a ordered_map

Revision en3, by Brackets12, 2026-11-08 00:43:56

I was trying to write an ordered map, which will work like a frequency table. I thought it would be a good addition because ordered_set doesn't support duplicate entries very well.

Here's some code I wrote:

Code

Basically, it changes the update policy to take the frequencies of the keys into account when calculating find_by_order and order_of_key.

However, in the end. I found that this wouldn't work. The tree in pb_ds was implemented only to update the internal data when the tree has changed, so it only operates when a node is added or deleted, and I think it looks at the key first, then puts in the values last. That means if I change the value (frequency of key), then call find_by_order, the answer will be completely wrong because they internal data didn't update. There's also times where I insert key value pairs and it didn't update my frequency into the internal data, resulting in wrong answers.

So in the end I figured it wouldn't work. It was a fairly fruitless exploration for me. But at least I found some good links, so if you are interested in PBDS, here they are:

https://ideone.com/jOxlgl (an example of custom defined operator) https://ideone.com/JlJ86R (print tree)

A few useful blogs: https://codeforces.me/blog/entry/11080 (basics) https://codeforces.me/blog/entry/13279 (creating custom policies, tutorial-ish) https://codeforces.me/blog/entry/110677 (sum of segment) https://neveropen.tech/interval-tree-using-gnu-tree-based-container/

GCC documentation? https://gcc.gnu.org/onlinedocs/libstdc++/manual/policy_data_structures_design.html#pbds.design.container.tree

Hopefully this is helpful if you're interested in learning about PBDS as well.

Tags pbds, trees

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en6 English Brackets12 2026-11-08 00:49:50 0 (published)
en5 English Brackets12 2026-11-08 00:49:22 147
en4 English Brackets12 2026-11-08 00:45:38 14
en3 English Brackets12 2026-11-08 00:43:56 9
en2 English Brackets12 2026-11-08 00:41:24 50
en1 English Brackets12 2026-11-08 00:40:01 3918 Initial revision (saved to drafts)