简体   繁体   English

为什么向量的.at()成员函数返回引用而不是迭代器?

[英]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. 对于标准库,这是一个清晰,简单且众所周知的设计决策。

Element access 元素访问

  • 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直接访问基础数组

Iterators 迭代器

  • 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.

相关问题 为什么std :: find for vector返回迭代器而不是整数位置 - Why does the std::find for vector return a iterator instead of the integer position 返回对vector成员变量的引用 - Return reference to a vector member variable 为什么 std::iterator 不包含 std::prev() 作为成员函数? - Why does std::iterator not contain std::prev() as a member function? 为什么通用引用必须对成员函数使用模板而不是类? - Why universal reference have to use template for the member function instead of class? 为什么函数返回不移动向量? - Why does function return not move vector? 为什么递归 function 中的迭代器在返回后指向开头? - Why does an iterator in a recursive function point to the beginning after a return? C++ - 为什么从成员 function 返回对 class 的引用 - C++ - why return a reference to the class from a member function 为什么传递给函数set :: iterator而不是const_iterator违反了One Definition Rule? - Why does passing to a function a set::iterator instead of a const_iterator violate the One Definition Rule? 成员函数返回成员变量的右值引用 - Member function return rvalue reference of member variable 为什么此向量迭代器不会失效? - Why does this vector iterator not become invalidated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM