封装std :: map允许迭代,但是没有直接键访问? [英] Encapsulate std::map to allow iterating, but no direct key access?

查看:88
本文介绍了封装std :: map允许迭代,但是没有直接键访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以将std::map封装在用户定义的类中,从而允许迭代,但不允许直接访问键?

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.

推荐答案

请勿公开迭代器.公开for_each函数,该函数将每个元素传递给用户提供的(lambda)函数.

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

或者,使用转换迭代器.

这篇关于封装std :: map允许迭代,但是没有直接键访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆