迭代 C++ 映射中的键

2022-01-07 00:00:00 c++ stl

有没有办法迭代键而不是 C++ 映射对?

Is there a way to iterate over the keys, not the pairs of a C++ map?

推荐答案

如果您确实需要隐藏真实"迭代器返回的值(例如,因为您想使用具有标准算法的密钥迭代器,以便它们对键而不是对进行操作),然后看看 Boost 的 transform_iterator.

If you really need to hide the value that the "real" iterator returns (for example because you want to use your key-iterator with standard algorithms, so that they operate on the keys instead of the pairs), then take a look at Boost's transform_iterator.

[提示:查看新类的 Boost 文档时,请先阅读末尾的示例".然后,您就有机会弄清楚其余部分到底在谈论什么:-)]

[Tip: when looking at Boost documentation for a new class, read the "examples" at the end first. You then have a sporting chance of figuring out what on earth the rest of it is talking about :-)]

相关文章