简体   繁体   English

迭代器在vector :: begin()中的返回

[英]Iterator return in vector::begin()

I don't quite get what vector::begin() actually returns. 我不太了解vector::begin()实际返回了什么。

Cplusplus.com tells me that vector::begin() returns the iterator of the vector, which means that for vector<int> v; Cplusplus.com告诉我vector::begin()返回向量的迭代器,这意味着对于vector<int> v; , it will give 0. ,它将得出0。

However, when I debug on Visual Studio, the watch table showed me the value of the first element of v . 但是,当我在Visual Studio上调试时,监视表向我显示了v的第一个元素的值。 Moreover, the "Type" column denoted that it was a std::_Vector_iterator . 此外,“类型”列表示它是std::_Vector_iterator

As a consequence, what actually is the output of vector::begin() ? 结果, vector::begin()的输出实际上是什么?

std::vector::begin returns an iterator. std::vector::begin返回迭代器。 An iterator is a generalisation of a pointer — it's a type which can be used to access elements of a container. 迭代器是指针的一般化-它是一种可用于访问容器元素的类型。 Each container provides its own iterator type. 每个容器都提供自己的迭代器类型。 How exactly iterators are represented is an implementation detail, you as a programmer should only care about their interface (and guarantees they make about stability, validity, etc. See suitable documentation for more info). 如何精确地表示迭代器是实现细节,作为程序员,您应该只关心它们的接口(并保证它们具有稳定性,有效性等优点。有关更多信息,请参见适当的文档 )。

Different types of iterators support different operations: some can only be incremeneted (can only move forward), some can also be decremented (move backward); 不同类型的迭代器支持不同的操作:某些只能迭代(只能向前移动),有些也可以递减(向后移动); some need to move one step at a time, some can move by multiple elements in one go; 有些需要一次移动一步,有些可以一次移动多个元素。 etc. 等等

The iterators provided by std::vector are random-access and contiguos, so they function almost exactly the same as pointers do. std::vector提供的迭代器是随机访问和重叠的,因此它们的功能几乎与指针完全相同。

For an empty std::vector , calling begin() will give you the exact same iterator as is returned by calling end() . 对于空的std::vector ,调用begin()将为您提供与调用end()返回的迭代器完全相同的迭代器。 end() always returns a special, past-the-end iterator. end()始终返回一个特殊的, 过去的迭代器。 That iterator is not dereferenceable (it doesn't point to any element), it just serves as an indicator of the container's end. 该迭代器不可解除引用(它不指向任何元素),而只是用作容器结束的指示符。

Cplusplus.com tells me that vector::begin() returns the iterator of the vector, Cplusplus.com告诉我vector::begin()返回矢量的迭代器,

That is correct. 那是对的。

which means that for vector<int> v; 这意味着对于vector<int> v; , it will give 0. ,它将得出0。

That is an incorrect conclusion from the previous statement. 从先前的陈述中得出的结论是错误的。 std::vector<T>::begin() returns an iterator that can be used to access the contents of the first element of the object, if it is not empty. std::vector<T>::begin()返回一个迭代器,如果它不为空,则该迭代器可用于访问对象的第一个元素的内容。

Given, 鉴于

std::vector<int> v;
auto iter = v.begin();
int b = *iter; // Problem since the vector is empty.

and

std::vector<int> v{1, 3, 10};
auto iter = v.begin();
int b = *iter; // OK. b is the first element of the vector
int c = v[0];  // OK. c is also the first element of the vector.

Perhaps you confusion stems from the fact that the last two lines above evaluate to the same value. 也许您会感到困惑,是因为以上最后两行的值相同。

However... 然而...

std::vector<T>::iterator is a type in the template std::vector . std::vector<T>::iterator是模板std::vector的类型。 It is a RandomAccessIterator . 它是一个RandomAccessIterator You can read more about it, ie RandomAccessIterator , at http://en.cppreference.com . 您可以在http://en.cppreference.com上了解有关它的更多信息,即RandomAccessIterator

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

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