Encapsulate std::map to allow iterating, but no direct key access? [duplicate]

我是研究僧i 提交于 2019-12-08 10:56:34

问题


Is there a way to encapsulate std::map within a user-defined class, allowing iteration, but not allowing direct access to the key?

I would like to manipulate the key upon insertion and I want to force all access to elements through a user-defined method (as the key will need to be modified).

I think the problem is allowing iterating over the map, whilst preventing the user from accessing the key via the iterator.


回答1:


Do not expose iterators. Expose a for_each function that passes each element to the user-provided (lambda) function.

Alternatively, use a transform iterator.



来源:https://stackoverflow.com/questions/51862149/encapsulate-stdmap-to-allow-iterating-but-no-direct-key-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!