简体   繁体   English

它 - vector.begin() 实际上是做什么的?

[英]What does it - vector.begin() actually do?

So from what I learned to get the position of an iterator in a vector we do this:因此,根据我学到的在向量中获取迭代器的 position 的知识,我们这样做:

it - vector.begin();

Can someone explain what this minus 'actually' does?有人可以解释这个减号“实际上”的作用吗?

Presumably vector is an instance of std::vector and it is an iterator to an element of that container.大概vectorstd::vector的一个实例, it是该容器元素的迭代器。

Given two random access iterators to the same range, subtracting one from the other results in the distance from one element to their sibling.给定两个相同范围的随机访问迭代器,从另一个中减去一个会导致一个元素到其兄弟元素的距离。 The result is same as if you would have subtracted the index of one element from the index of the other element.结果与从另一个元素的索引中减去一个元素的索引相同。

The distance works like the number line: Distance from lower index to higher is negative.距离就像数字线一样:从低指数到高指数的距离是负数。

begin returns an iterator to the first element of a container. begin返回一个指向容器第一个元素的迭代器。 The index of the first element is 0. Thus, subtracting begin iterator from another iterator results in the distance of the other iterator from the beginning, which is the same as the index of the element pointed by the iterator.第一个元素的索引为 0。因此,从另一个迭代器中减去 begin 迭代器会导致另一个迭代器与开头的距离,这与迭代器指向的元素的索引相同。

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

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