[英]Why does .at() member function of a vector return a reference instead of an iterator?
Up until a few days ago I thought all 'position' related member functions of a vector
returned an iterator
. 直到几天前,我还认为vector
所有与“位置”相关的成员函数都返回了iterator
。 I found out recently that while insert()
and erase()
functions do indeed return iterators
, begin()
and end()
do so by definition, but functions like at()
, front()
, back()
do not do so, they return a simple reference. 我最近发现,虽然insert()
和erase()
函数确实确实返回了iterators
,但begin()
和end()
确实按定义返回了iterators
,但是at()
, front()
, back()
类的函数却没有这样做,他们返回一个简单的参考。
While references make life easier as I would not have to dereference the iterator
fist, to me it still seems inconsistent that some member functions are returning a reference instead of an iterator. 虽然引用使生活变得更加轻松,因为我不必取消引用iterator
拳头,但对我来说,仍然有些矛盾,某些成员函数正在返回引用而不是迭代器。 If anything, C++ tries to minimize inconsistencies like this by providing the bare minimum while still maintaining ease in programming. 如果有的话,C ++会通过提供最低限度的方法来尽量减少此类不一致性,同时仍保持编程的简便性。
at
method is from the group of common container methods called 'Element access', those return reference, pointers. at
方法来自一组称为“元素访问”的常见容器方法,这些方法返回引用,指针。 There is another group of common container methods called 'Iterators', those return iterators. 还有另一组称为“迭代器”的通用容器方法,这些方法返回迭代器。 It is clear, simple and well-known design decision for the standard library. 对于标准库,这是一个清晰,简单且众所周知的设计决策。
at
: access specified element with bounds checking at
:通过边界检查访问指定的元素 operator[]
: access specified element operator[]
:访问指定的元素 front
: access the first element front
:访问第一个元素 back
: access the last element back
:访问最后一个元素 data
direct access to the underlying array data
直接访问基础数组 begin
/ cbegin
returns an iterator to the beginning begin
/ cbegin
将迭代器返回到开头 end
/ cend
returns an iterator to the end end
/ cend
将迭代器返回到末尾 rbegin
/ crbegin
returns a reverse iterator to the beginning rbegin
/ crbegin
将反向迭代器返回到开头 rend
/ crend
returns a reverse iterator to the end rend
/ crend
将反向迭代器返回到末尾 In the iterator concept, elements within iterator range are accessed through std::advance
method of STD. 在迭代器概念中,可通过STD的std::advance
方法访问迭代器范围内的元素。 This would work for InputIterator
s, for BST, list, vectors, etc., of course with different complexity. 这当然适用于InputIterator
,BST,列表,向量等,当然具有不同的复杂性。
begin()
, end()
, insert()
, erase()
, etc. are methods that work on the vectors' sequence of elements itself, while operator []
, at()
, front()
, and back()
are methods that access concrete elements of this sequence. begin()
, end()
, insert()
, erase()
等是对向量本身的元素序列起作用的方法,而operator []
, at()
, front()
和back()
是方法访问此序列的具体元素。 I don't really see an inconsistency here. 我在这里看不到任何不一致之处。 They exist for all sequence containers and they always do conceptually the same thing. 它们存在于所有序列容器中,并且在概念上总是做相同的事情。 Sure, you could implement something like front()
and back()
yourself, nothing keeps you from doing so. 当然,您可以自己实现诸如front()
和back()
东西,没有什么可以阻止您这样做。 They are by definition equivalent to dereferencing begin()
and prev(end())
respectively. 根据定义,它们等效于分别取消引用begin()
和prev(end())
。 They exist for convenience. 它们的存在是为了方便。 Depending on how far you want to go, std::vector
itself exists just for convenience… 根据您想走多远, std::vector
本身的存在只是为了方便……
The abstraction that the Standard Template Library presents is sequences , iterators , and algorithms . 标准模板库提供的抽象是序列 , 迭代器和算法 。 Container are one way of creating and managing sequences, but they are not the only way. 容器是创建和管理序列的一种方法,但不是唯一的方法。 For example, an input stream can be used as a sequence, by creating a std::istream_iterator
. 例如,通过创建std::istream_iterator
,可以将输入流用作序列。 But things like int i; double d; std::cin >> i >> d;
但是像int i; double d; std::cin >> i >> d;
这样的东西int i; double d; std::cin >> i >> d;
int i; double d; std::cin >> i >> d;
would be rather awkward to write if std::cin
only defined an iterator-based interface. 如果std::cin
仅定义了一个基于迭代器的接口,则编写起来会很尴尬。 Same thing for containers: they're useful in contexts other than iteration, and they define interfaces appropriate to their general uses; 容器也是一样:它们在迭代以外的环境中很有用,并且它们定义了适合其一般用途的接口; one of those uses is as a sequence for STL algorithms, but there are other uses, too. 这些用途之一是作为STL算法的序列,但也有其他用途。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.