简体   繁体   English

在C ++中,从数组元素的指针获取索引的最快方法是什么?

[英]What is the fastest way to obtain an index from a pointer of an array element in C++?

Suppose you have a pointer to an int from an array. 假设您有一个指向数组中int的指针。 Is this the best way to find out which item of the array the pointer points to? 这是找出指针指向数组的哪一项的最佳方法吗?

int nmbr = pointer - &array[0];
cout<<"Item number "<<nmbr+1;

If the pointer pointed to the 6th item, would this always print "Item number 6"? 如果指针指向第六项,是否总是打印“第六项”? Is this error-prone? 这容易出错吗? Is there a faster way to do it? 有更快的方法吗? Iterating over the array won't work because of possible duplicate values, but does this always work? 由于可能存在重复值,因此无法对数组进行迭代,但这是否总是可行? In this situation we assume the array starts from item 0 to item x. 在这种情况下,我们假设数组从项目0开始到项目x。 Is there a situation in which an array's items aren't stored in a continuous line (if the array was initialized as int array[x]; )? 是否存在数组的项目没有连续存储的情况(如果数组初始化为int array[x]; )?

Assuming pointer is known to point at an element of array , the 假设已知pointer array的元素,则

  std::ptrdiff_t nmbr = pointer - array;

will do it. 会做的。 If pointer does not point at an element of the array, the behaviour is undefined. 如果指针未指向数组的元素,则行为未定义。 std::ptrdiff_t is declared in the standard header <cstddef> , and is an implementation-defined signed integral type. std::ptrdiff_t在标准头文件<cstddef> ,并且是实现定义的带符号整数类型。 Using int is not a good idea, particularly for large arrays, since std::ptrdiff_t may be able to represent a larger range of values than an int (and, if the value is larger than an int can represent, converting the result to int also gives undefined behaviour). 使用int并不是一个好主意,尤其是对于大型数组,因为std::ptrdiff_t可能表示的值范围比int (并且,如果该值大于int可以表示的值,则将结果转换为int还给出了不确定的行为)。

The +1 s in your code are incorrect, since array indexing in C++ is zero based. 您代码中的+1不正确,因为C ++中的数组索引是从零开始的。

All arrays, in the sense of something declared as 所有数组,在某种意义上声明为

  some_type array[some_positive_value];

have contiguous elements. 具有连续的元素。 Note that C++ does not support C's VLAs (variable length arrays) ie some_positive_value must be known at compile time. 请注意,C ++不支持C的VLA(可变长度数组),即必须在编译时知道some_positive_value

Both +1's look wrong.. But yes, this is the fastest and simplest way. +1看上去都错了。。但是,是的,这是最快,最简单的方法。 It's guaranteed to work. 保证可以正常工作。

As mentioned by XTF, yes it is guaranteed to work. 如XTF所述,是的,它可以保证正常工作。 There is however, no need for taking the address of the 0'th element, just use array as it is. 但是,不需要使用第0个元素的地址,只需按原样使用array

For a more generic solution, that will also work on other containers you could consider the following examples: 对于更通用的解决方案,该解决方案也适用于其他容器,您可以考虑以下示例:

#include <iostream>
#include <iterator>
#include <array>

int main()
{
    {
        int array[10];
        int * p = &array[5]; // Somehow get a pointer to the sixth element
        std::cout << std::distance(std::begin(array), p) << std::endl;
    }

    {
        std::array<int, 10> array;
        int * p = &array[5]; // Somehow get a pointer to the sixth element
        std::cout << std::distance(std::begin(array), p) << std::endl;
    }
}

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

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