简体   繁体   English

为什么不同类的STL算法调用不同?

[英]Why are STL algorithms called differently for different classes?

I am currently looking at STL libraries and am wondering why for a vector class vector<string> names; 我目前正在查看STL库,我想知道为什么对于矢量类vector<string> names; I have to call remove(); 我必须调用remove(); as follows: 如下:

names.erase(remove(names.begin(), names.end(), "Simon"), names.end());

While when using a list class list<string> names; 当使用列表类list<string> names; I can call the function as follows: 我可以调用函数如下:

remove("Simon");

I also notice the same for reverse(); 我也注意到reverse(); for vector<string> names; for vector<string> names; it is called as follows: 它被称为如下:

reverse(names.begin(), names.end());

While for list<string> names; 而对于list<string> names; it is called as follows: 它被称为如下:

names.reverse();

Is it more proper to always call the way the vector would? 总是调用矢量的方式更合适吗? why is this? 为什么是这样? I'm very new to C++ so I'm keen to know the best way to do things. 我对C ++很陌生,所以我很想知道最好的做事方式。

Basically, there are some special cases that have to do with the nature of specific containers. 基本上,有一些特殊情况与特定容器的性质有关。

In general the free functions std::remove , std::remove_if , and std::reverse declared in the <algorithm> header will work on vectors, lists, deques, and arrays by copying and moving elements. 通常 ,在<algorithm>头中声明的自由函数std::removestd::remove_ifstd::reverse将通过复制和移动元素对矢量,列表,deques和数组起作用。 (They will not, of course, work on sets or maps, since for those you are not free to rearrange elements willy-nilly.) Note that std::remove does not delete elements from containers. (当然,它们不会在集合或映射上工作,因为对于那些你不能随意重新排列元素的人。)请注意, std::remove不会从容器中删除元素。

In general the member function erase of each container type is used to delete elements from that container. 通常 ,每个容器类型的成员函数erase用于从该容器中删除元素。 (Note that std::array does not have erase because its size is fixed.) (注意, std::array没有erase因为它的大小是固定的。)

There are special cases: 特殊情况:

  • std::list provides reverse , as a member because only the member function can guarantee that it does not invalidate any iterators; std::list提供reverse ,作为成员,因为只有成员函数可以保证它不会使任何迭代器无效; the generic std::reverse cannot. 通用的std::reverse不能。
  • The same goes for remove and remove_if though the names are misleading because unlike the free functions, the members do delete elements from the list. removeremove_if也是如此,虽然这些名称具有误导性,因为与自由函数不同,成员从列表中删除元素。
  • There is also a member sort for std::list because the generic std::sort only works on random access iterators. 还有std::list的成员sort ,因为通用std::sort仅适用于随机访问迭代器。
  • For sets and maps we should use their member lower_bound , upper_bound , equal_range , and count instead of the generic versions, since they know how to walk down the tree and get the result in logarithmic time whereas the free functions will use linear time. 对于集合和映射,我们应该使用它们的成员lower_boundupper_boundequal_rangecount而不是泛型版本,因为它们知道如何沿着树向下走并以对数时间获得结果,而自由函数将使用线性时间。

Generally, the principle appears to be: the standard library containers support uniform interfaces as far as possible, but also provide additional specialized functions in order to provide functionality that depends on their internals. 通常,原则似乎是:标准库容器尽可能支持统一接口,但也提供额外的专用功能,以便提供依赖于其内部的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM