这是与此问题相关的一种元问题:How do I write a branchless std::vector scan? .
最初提出的问题(强调我的问题):
I have a
std::vector<int> data
; and I want to find all the array indexes that are less than 9 and add them to a result vector.
然后我将其编辑为:
I have a
std::vector<int> data
, and I want to find all elements that are less than 9 and add them to a result vector.
然后另一个用户将其编辑为:
I have a
std::vector<int> data
and I want to find all array indices at which the elements are less than 9 and add them to a result vector.
我将其标记为请版主恢复此设置,并指出:
I reworded this question (stackoverflow.com/posts/38798841/revisions) with the main goal of replacing "array indexes" with "elements", since that is what's actually being asked - and referring to "indexes/indices" in this context causes some confusion. However, user Aconcagua has overturned this crucial part of my edit. It's my belief that his latest edit should be rolled back.
并且被拒绝,原因如下:
Aconcagua's edit is correct; the user is collecting array indicies, not the array elements themselves
。
现在,我不太明白主持人所说的内容 - “用户正在收集数组索引”。在我看来,索引是数组中元素的位置。例如,在C语言中:
char array[] = {'t', 'e', 's', 't'};
int index = 1;
char element = array[index];
printf('%c', element);
我根本不明白他如何或为何收集“索引”而不是“元素”。有人可以澄清这一点以便我能够真正理解吗?
最佳答案
在链接问题顶部的示例代码中:
for (int i = 0; i < data.size(); ++i)
if (data[i] < 9)
r.push_back(i);
请注意 i
正在被添加到 vector 中,不是 data[i]
.
i
是感兴趣元素的索引,并且是正在存储的内容,而不是存储在 data[i]
的值,这是 data
中的元素 vector 。
关于c++ - 数组索引与数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39260851/