c++ - 数组索引与数组元素

标签 c++ c arrays indices

这是与此问题相关的一种元问题: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/

相关文章:

c - 如何在 ubuntu 12.04 下安装 libnet 的手册页

java - 使用 Java 进行 RTSP 编程。如何读取单部分框架?

c++ - 使用 boost 读取复杂的 JSON 数组

c++ - Boost::Property_Tree 设置

c++ - 相同的地址被存储在链表的头部

c++ - 数组未按 C++ 中的预期填充

c++ - 将数组从 C/C++ 传递到 Fortran 并返回计算后的数组

字符数组 vector 上的 C++ GCC 4.3.2 错误

c - 完全忽略 C 函数的可变参数是否安全?

C - 从字符串的中间开始遍历