简体   繁体   English

为什么 std::iterator 不包含 std::prev() 作为成员函数?

[英]Why does std::iterator not contain std::prev() as a member function?

it++;                       // OK    : Widely used expression for moving iterator.

it_prev = it-1;             // ERROR : What I expected; + - operators never existed
it_prev = std::prev(it)     // OK

it_next3 = it+3;            // ERROR : also, nothing like this exists
it_next3 = std::next(it,3)  // OK

Why does the Iterator class not have + - operators as member functions?为什么 Iterator 类没有 + - 运算符作为成员函数?

Or std::prev() as a member function to do this?或者std::prev()作为成员函数来做到这一点?

it_prev = it.prev()         // nothing like this

Is there a special reason for defining the prev function external to the iterator?在迭代器外部定义prev函数是否有特殊原因?

If prev() were a member function instead of a free function , it would be less generic since it won't be possible to work with built-in types (eg, pointers) as iterators:如果prev()是一个成员函数而不是一个自由函数,那么它的通用性就会降低,因为它不可能使用内置类型(例如,指针)作为迭代器:

int *ptr = ...
// ...
ptr.prev() // <-- Pointers don't have member functions!!

whereas with a non-member function template , such as std::prev() , all is needed for it to work with pointers is a specialization that deals with pointers:而对于非成员函数模板,例如std::prev() ,它与指针一起工作所需要的只是一个处理指针的特化:

int *ptr = ...
// ...
std::prev(ptr); // <-- OK

Pointers also support the increment and decrement operators (ie, ++ and -- ), so defining them in iterator classes doesn't hinder generic programming.指针还支持递增递减运算符(即++-- ),因此在迭代器类中定义它们不会妨碍泛型编程。 The same reasoning applies to the binary operators + and - .同样的推理适用于二元运算符+-

Random access iterators do have operator+ overloaded.随机访问迭代器确实有operator+重载。 But others don't since something like it + n would have a linear complexity and most people don't expect that, especially considering iterators are also meant to be pointer-like for STL containers.但其他人没有,因为像it + n这样的东西会具有线性复杂性,而且大多数人并不期望这一点,特别是考虑到迭代器对于 STL 容器来说也应该是类似指针的。 That is also the reason why they don't have a member function called prev since pointers being valid iterator implementations is very convenient, efficient and desired.这也是为什么他们没有名为prev的成员函数的原因,因为指针是有效的迭代器实现非常方便、高效和需要。 If you want to advance the iterator and don't mind the complexity, you can do that explicitly with std::advance :如果您想推进迭代器并且不介意复杂性,您可以使用std::advance明确地做到这一点:

std::advance标准::提前

[...] [...]

Increments given iterator it by n elements.给迭代器它增加 n 个元素。

If n is negative, the iterator is decremented.如果 n 为负,则迭代器递减。 In this case, InputIt must meet the requirements of LegacyBidirectionalIterator , otherwise the behavior is undefined.在这种情况下,InputIt必须满足的要求LegacyBidirectionalIterator ,否则该行为是不确定的。

[...] [...]

Complexity复杂

Linear.线性。
However, if InputIt additionally meets the requirements of LegacyRandomAccessIterator , complexity is constant.但是,如果InputIt另外符合要求LegacyRandomAccessIterator ,复杂性是恒定的。

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

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