[英]Iterate over vector using loop c++
Let's say I have a vector of long long elements:假设我有一个 long long 元素的向量:
std::vector<long long>V {1, 2, 3};
I have seen that in order to iterate over the vector you can do this:我已经看到,为了迭代向量,你可以这样做:
for (auto i = v.begin() ; i != v.end() ; i++)
cout<<*i;
i++
means i
grows by 1
, but shouldn't the address go up 8 bytes to print the next element? i++
意味着i
增长1
,但地址 go 不应该增加 8 个字节来打印下一个元素吗? So "growing" part of the for loop(for any type) should look like this:所以 for 循环的“增长”部分(对于任何类型)应该如下所示:
i += sizeof(v[0]);
I'm assuming an address can hold 1 byte, so if the starting address of an integer would be 1000, then its total allocation will be represented by adresses:1000, 1001, 1002, 1003.I would like to understand memory better so I'd be thankful if you could help me.我假设一个地址可以容纳 1 个字节,所以如果 integer 的起始地址是 1000,那么它的总分配将由地址表示:1000、1001、1002、1003。我想更好地理解 ZCD69B4957F06CD81817BF3D619ZE如果你能帮助我将不胜感激。
When you increment a pointer it goes up by the "size of" the pointer's type.当您增加指针时,它会增加指针类型的“大小”。 Remember, for a given pointer
i
, i[0]
is the first element and is equivalent to *(i + 1)
.请记住,对于给定的指针
i
, i[0]
是第一个元素,等效于*(i + 1)
。
Iterators tend to work in a very similar fashion so their operation is familiar, they feel like pointers due to how operator*
and operator->
are implemented.迭代器倾向于以非常相似的方式工作,因此它们的操作很熟悉,由于
operator*
和operator->
的实现方式,它们感觉就像是指针。
In other words, the meaning of i++
depends entirely on what i
is and what operator++
will do on that type.换句话说,
i++
的含义完全取决于i
是什么以及operator++
将对该类型做什么。 Seeing ++
does not automatically mean +1
.看到
++
并不自动意味着+1
。 For iterators it has a very specific meaning, and that depends entirely on the type.对于迭代器,它具有非常特定的含义,这完全取决于类型。
For std::vector
it moves a pointer up to the next entry.对于
std::vector
,它将指针向上移动到下一个条目。 For other structures it might navigate a linked list.对于其他结构,它可能会导航链接列表。 You could write your own class where it makes a database call, reads a file from disk, or basically whatever you want.
您可以编写自己的 class 进行数据库调用,从磁盘读取文件,或者基本上任何您想要的。
Now if you do i += sizeof(v[0])
instead then you're moving up an arbitrary number of places in your array.现在,如果您执行
i += sizeof(v[0])
,那么您将在数组中向上移动任意数量的位置。 It depends on what the size of that entry is, which depends a lot on your ISA .这取决于该条目的大小,这在很大程度上取决于您的ISA 。
std::vector
is really simple, it's just a straight up block of memory treated like an array. std::vector
非常简单,它只是一个像数组一样处理的 memory 的直线块。 You can even get a pointer to this via the data()
function.您甚至可以通过
data()
function 获得指向它的指针。
In other words think of i++
as "move up one index" not "move up one byte".换句话说,将
i++
视为“上移一个索引”而不是“上移一个字节”。
One of the nice things about the way you've written this code is that you don't need to worry about bytes.您编写此代码的方式的好处之一是您无需担心字节。
Your auto
type for i
is masking the fact that it's a vector iterator, in other words, a special type that is designed for accessing members of a vector of long long
.您的
i
auto
类型掩盖了它是向量迭代器的事实,换句话说,是一种特殊类型,旨在访问long long
向量的成员。 This means that *i
evaluates to a long long
pulled from whichever member of the vector i
is currently indexing, so i++
advances i
to index the next member of the vector.这意味着
*i
评估为从向量i
当前正在索引的任何成员中提取的long long
,因此i++
推进i
以索引向量的下一个成员。
Now, in terms of low level implementation, the values 1, 2, 3
are probably sitting in adjacent 8-byte blocks of memory, and i
is probably implemented as a pointer, and incrementing i
is probably implemented by adding 8 to its value, but each of those assumptions is going to depend on your architecture and the implementation of your compiler.现在,就低级实现而言,值
1, 2, 3
可能位于 memory 的相邻 8 字节块中,并且i
可能实现为指针,递增i
可能通过将其值加 8 来实现,但是这些假设中的每一个都将取决于您的架构和编译器的实现。 And as I said, it's something you probably don't need to worry about.正如我所说,这可能是您无需担心的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.