为什么不在c ++容器中实现包含功能? [英] Why not implement contains function in c++ Containers?

查看:81
本文介绍了为什么不在c ++容器中实现包含功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初的问题是:为什么在C ++中, contains()函数在 Containers 中会丢失?

My initial question was: why does in C++ the contains() function miss in Containers?

所以我寻找了一个解释,我发现了一个有趣的地方,为什么在所有容器中未实现某些其他功能(主要是由于性能问题和便利性)。

So I looked for an explanation and I found something interesting about why some other function are not implemented in all the Containers (essentially because of performance issues and convenience).

我知道您可以使用算法查找函数c>库,或者您可以只使用 Iterator 编写自己的函数,但我不明白的是为什么要使用 set ,例如,实现了包含函数(在其中称为 find ),而在 vector queue 不是

I know that you can use the find function from algorithm library or you can just write your own function with Iterator, but what I can't understand is why in set, for example, the contains function(where it's called find) is implemented, whereas in vector or queue it is not.

我也很清楚为什么容器类不共享Java中的 Collections 这样的通用接口(感谢答案),但在这种情况下,我找不到为什么不在所有Containers类中(或至少在类似 vector 的某些类中)实现 contains()函数的原因。

It's pretty clear to me too why Container classes does not share a common interface like Collections do in Java (thanks to this answer) but in this case I can't find the reason why not implement the contains() function in all the Containers classes (or at least in some like vector).

谢谢

推荐答案

容器有一个很好的理由做不共享公共(继承)接口(例如在 Java 中),这就是使 C ++ 泛型如此的原因强大。如果对所有容器只能编写一次代码,为什么还要为每个容器编写代码呢?这是 STL 建立的主要原则之一。

There is a good reason that containers do not share a "common (inherited) interface" (like in Java) and it is what makes the C++ generics so powerful. Why write code for every container when you can write it only once for all containers? This is one of the main principles the STL was built on.

如果使用依赖于a成员函数的容器常见的继承接口,您必须为每个容器编写一个 find 函数。那是浪费和糟糕的工程。好的设计表明,如果您只能在一个地方编写代码,则应该这样做,因为您只需要从那个地方删除错误,而在一个地方修复错误则可以无处不在修复该错误。

If using containers relied on member functions from a common inherited interface you would have to write a find function for every container. That is wasteful and poor engineering. Good design says if you can write code in only one place you should because you only have to remove the bugs from that one place, and fixing a bug in one place fixes the bug everywhere.

因此, STL 背后的原理是将算法分开 容器,因此您只需编写一次算法即可,它适用于 all 容器。一旦调试了算法,就会为所有容器进行调试。

So the philosophy behind the STL is to separate the algorithms from the containers so that you only have to write the algorithm once and it works for all containers. Once the algorithm is debugged, it is debugged for all containers.

这种美中不足之处在于容器由于其内部结构而可以做出更有效的决策。对于那些容器,添加了 typespecific 函数,可以利用该效率。

A fly in that ointment is that some containers can make more efficient decisions due to their internal structure. For those containers a type specific function has been added which will take advantage of that efficiency.

但是大多数函数应该 >与容器分开。它称为 decoupling ,它在促进代码重用的同时减少了错误,通常比 polymorphism 更重要,而 polymorphism 就是 Java 使用容器(一个通用的继承接口)。

But most functions should be separate from the containers. It is called decoupling and it reduces bugs while promoting code reuse, often much more so than polymorphism which is what libraries like Java containers use (a common inherited interface).

这篇关于为什么不在c ++容器中实现包含功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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