Given an std::map<K,V> of size n, can you randomly get a key (and value) in the container, portably? Randomly here refers to key's position in the container. Let's say the distribution has to be only close to uniform.
My thinking was that standard library maps are often (always?) implemented as red-black trees, which are decently balanced. So locating an (almost-)uniformly random value could be done in log-time if you can access this underlying structure. It wouldn't be trivial but it could be done (idea: each node can estimate roughly how many child-nodes are left and how many are right of it).
In general one cannot access that information, so the obvious solution is to linearly iterate over it until you find the ith element for some uniformly-random chosen 0 <= i < n. However, as of C++17 there is also the rudimentary node interface that allows one to extract and re-insert nodes. But after some thought I don't see how this would be helpful.
Is there something I am overlooking or is this truly impossible in sub-linear time given the standardised interface of std::map?
Note: Obviously, if one wants to get more than, say, n / log n elements, shuffling a vector of map iterators is likely the best option.
You know exactly how many children the root has. From this you can roughly estimate how many children either of its children have, and more generally, how many children any node has, depending on its height.
From this data it is possible to generate a path in the tree that leads to a roughly uniformly selected node.
In order to follow the path, we need further assumptions: that the search starts from the root; and that the decision to go left, go right, or return the current node is made after calling the comparison function once or twice. It is easy to test that these assumptions hold by checking them from within a custom comparator. So a custom comparator can be made that leads the search along the predetermined path.
(This uses ideas from a now-deleted answer)