c++如何初始化数组的const元素。

时间:2021-05-29 21:30:50

i need a way to initialize const elements of an array for the program i am currently working on. The problem is that i have to initialize these elements with a function, there is no way to do it like this:

我需要一种方法来初始化当前正在处理的程序的一个数组的const元素。问题是我必须用一个函数初始化这些元素,没有办法这样做:

const int array[255] = {1, 1278632, 188, ...};

because its alot of data i have to generate. What i tried is to memcpy data to the const int's but that can't work and hasn't worked.

因为我要生成大量的数据。我所尝试的是将数据存储到const int中,但这行不通,也行不通。

 const int array[255];

 void generateData(){
      for(int i = 0; i < 255; i++) {
          initializeSomehowTo(5, array[i]);
      }
 }

I hope you understand what i am trying, sorry if i doubled the question, i must have overlooked it.

我希望你能理解我在做什么,对不起,如果我把问题翻倍,我一定是忽略了。

3 个解决方案

#1


17  

How about this?

这个怎么样?

#include <array>
typedef std::array<int, 255> Array;

const Array array = generateData();

Array generateData(){
    Array a;
    for(int i = 0; i < a.size(); i++) {
        initializeSomehowTo(a[i]);
    }
    return a;
}

#2


6  

The easiest approach is to get the filled array from a function and use that to initialize your const (or constexpr) object. However, built-in arrays can't be copied but std::array<T, N> be:

最简单的方法是从函数中获取填充数组,并使用它初始化const(或constexpr)对象。但是,内置数组不能被复制,但是std::array 为: ,>

std::array<T, 255> array = initializeData();

If you need a built-in array, I can imagine initializing a static member of a class (template, actually) where the index is expanded from indices expanded from an std::make_index_sequence<255> and used as positional argument in the array, i.e., something along these lines:

如果您需要一个内置数组,我可以想象初始化一个类的静态成员(实际上是模板),其中索引是从std:::make_index_sequence<255>扩展的索引中展开的,并在数组中用作位置参数,例如。,也就是说:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <utility>

int some_function(std::size_t i) { return i; }

template <typename> struct initialized_array_base;
template <std::size_t... I>
struct initialized_array_base<std::index_sequence<I...>> {
    static const int array[sizeof...(I)];
};

template <std::size_t... I>
int const initialized_array_base<std::index_sequence<I...>>::array[sizeof...(I)]
    = { some_function(I)... };

struct initialized_array
    :initialized_array_base<std::make_index_sequence<256>> {
};

int main() {
    std::copy(std::begin(initialized_array::array),
              std::end(initialized_array::array),
              std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}

#3


3  

You can create a writable array, initialize it, and, then, create a const reference to it.

您可以创建一个可写数组,初始化它,然后创建对它的const引用。

int arry[255];

void generateData(){
  for(int i = 0; i < 255; i++) {
      initializeSomehowTo(5, arry[i]);
  }
}

const int (&array)[255] = arry;

#1


17  

How about this?

这个怎么样?

#include <array>
typedef std::array<int, 255> Array;

const Array array = generateData();

Array generateData(){
    Array a;
    for(int i = 0; i < a.size(); i++) {
        initializeSomehowTo(a[i]);
    }
    return a;
}

#2


6  

The easiest approach is to get the filled array from a function and use that to initialize your const (or constexpr) object. However, built-in arrays can't be copied but std::array<T, N> be:

最简单的方法是从函数中获取填充数组,并使用它初始化const(或constexpr)对象。但是,内置数组不能被复制,但是std::array 为: ,>

std::array<T, 255> array = initializeData();

If you need a built-in array, I can imagine initializing a static member of a class (template, actually) where the index is expanded from indices expanded from an std::make_index_sequence<255> and used as positional argument in the array, i.e., something along these lines:

如果您需要一个内置数组,我可以想象初始化一个类的静态成员(实际上是模板),其中索引是从std:::make_index_sequence<255>扩展的索引中展开的,并在数组中用作位置参数,例如。,也就是说:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <utility>

int some_function(std::size_t i) { return i; }

template <typename> struct initialized_array_base;
template <std::size_t... I>
struct initialized_array_base<std::index_sequence<I...>> {
    static const int array[sizeof...(I)];
};

template <std::size_t... I>
int const initialized_array_base<std::index_sequence<I...>>::array[sizeof...(I)]
    = { some_function(I)... };

struct initialized_array
    :initialized_array_base<std::make_index_sequence<256>> {
};

int main() {
    std::copy(std::begin(initialized_array::array),
              std::end(initialized_array::array),
              std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}

#3


3  

You can create a writable array, initialize it, and, then, create a const reference to it.

您可以创建一个可写数组,初始化它,然后创建对它的const引用。

int arry[255];

void generateData(){
  for(int i = 0; i < 255; i++) {
      initializeSomehowTo(5, arry[i]);
  }
}

const int (&array)[255] = arry;