为什么std :: pair暴露成员变量? [英] Why does std::pair expose member variables?

查看:170
本文介绍了为什么std :: pair暴露成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.cplusplus.com/reference/utility/pair/ ,我们知道 std :: pair 有两个成员变量,第一第二第一个

> / p>

解决方案

对于原始的C ++ 03 std :: pair



从C ++ 11和更高版本开始(我们现在使用C ++ 14,使用C ++ 17 ) std :: pair std :: tuple 的特殊情况,其中 std :: tuple 可以有任意数量的项目。因此,具有参数化的getter是有意义的,因为发明和标准化任意数量的项目名称是不切实际的。因此,您可以使用 std :: get std :: pair



所以,设计的原因是历史的, std :: pair 是向更通用性演变的最终结果。






其他新闻:



关于


; 据我所知,如果封装两个成员变量并给出 getFirst(); getSecond )


不,这是垃圾。



这就像说,锤子总是更好,无论你是在钉子,螺丝紧固,或修剪一块木头。特别是在最后一种情况下,锤子不是一个有用的工具。锤子可以是非常有用的,但这并不意味着他们是更好的一般来说:这只是废话。


From http://www.cplusplus.com/reference/utility/pair/, we know that std::pair has two member variables, first and second.

Why did the STL designers decide to expose two member variables, first and second, instead of offering a getFirst() and a getSecond()?

解决方案

For the original C++03 std::pair, functions to access the members would serve no useful purpose.

As of C++11 and later (we're now at C++14, with C++17 coming up fast) std::pair is a special case of std::tuple, where std::tuple can have any number of items. As such it makes sense to have a parameterized getter, since it would be impractical to invent and standardize an arbitrary number of item names. Thus you can use std::get also for a std::pair.

So, the reasons for the design are historical, that the current std::pair is the end result of an evolution towards more generality.


In other news:

regarding

As far as I know, it will be better if encapsulating two member variables above and give a getFirst(); and getSecond()

no, that's rubbish.

That's like saying a hammer is always better, whether you're driving in nails, fastening with screws, or trimming a piece of wood. Especially in the last case a hammer is just not a useful tool. Hammers can be very useful, but that doesn't mean that they're “better” in general: that's just nonsense.

这篇关于为什么std :: pair暴露成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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