如果你指的是同一个数组,为什么不只存储索引呢?
#include <limits>
#include <boost/static_assert.hpp>
int array[ARRAY_DIMENSION];
/**
* the following line will cause an error at compile-time if size_t
* is not enough to index the array.
*/
BOOST_STATIC_ASSERT(std::numeric_limits<size_t>::max() >= ARRAY_DIMENSION);
int access_array(size_t index)
{
size_t intended_index = array[index];
return array[intended_index];
}
Run Code Online (Sandbox Code Playgroud)