简体   繁体   English

C++ / Arduino:动态整数数组

[英]C++ / Arduino: dynamic int array

I'm writing a class for the Arduino.我正在为 Arduino 编写 class。 It's been going well so far, but I'm sort of stuck now...到目前为止一切顺利,但我现在有点卡住了......

I have declared an int array in my class我在 class 中声明了一个 int 数组

class myClass
{
  public: MyClass(int size);
  private:
    int _intArray[];
};

When I initialize the class MyClass myClass1(5) I need the array to look like this {0,0,0,0,0}.当我初始化 class MyClass myClass1(5)时,我需要数组看起来像这样 {0,0,0,0,0}。

My question: what do I need to do so that the array contains 'size' amount of zeros?我的问题:我需要做什么才能使数组包含“大小”数量的零?

MyClass::MyClass(int size)
{
    //what goes here to dynamically initialize the array
    for(int i=0; i < size; i++) _intArray[i] = 0;
}

Edit: Following up on various replies below, Arduino does not include the standard library so unfortunately std::vector is not an option编辑:跟进下面的各种回复,Arduino 不包括标准库,所以不幸的是std::vector不是一个选项

You should use a std::vector. 你应该使用std :: vector。

class myCLass {
public:
    myClass(int size)
        : intarray(size) {
    for(int i = 0; i < size; i++) intarray[i] = 0;
    }
private:
    std::vector<int> intarray;
};

I'll try the following: 我会尝试以下方法:

class myClass
{
  public: 
    MyClass(int size);
    ~MyClass();
  private:
    int* _intArray;
};

MyClass::MyClass(int size) {
  _intArray = new int[size];
  for (int i=0; i<size; ++i) _intArray[i] =0; // or use memset ...
}

MyClass::~MyClass() {
  delete[] _intArray;
}

Or, even better, use a STL vector instead ... 或者,更好的是,使用STL vector代替......

Your code as I'm writing this: 你写的代码是我的代码:

class myClass
{
  public: MyClass(int size);
  private:
    int _intArray[];
};

The declaration of _intArray is not valid C++: a raw array needs to have a size specified at compile time. _intArray的声明无效C ++:原始数组需要在编译时指定大小。

You can instead instead use a std::vector : 相反,您可以使用std::vector

class myClass
{
public:
    MyClass( int size )
        : intArray_( size )    // Vector of given size with zero-valued elements.
    {}

private:
    std::vector<int> intArray_;
};

Note 1 : some compilers may allow your original code as a language extension, in order to support the "struct hack" (that's a C technique that's not necessary in C++). 注1 :一些编译器可能允许您的原始代码作为语言扩展,以支持“struct hack”(这是C语言中不需要的C技术)。

Note 2 : I've changed the name of your member. 注2 :我已经更改了你的会员的名字。 Generally underscores at the start of names can be problematic because they may conflict with names from the C++ implementation. 通常,名称开头的下划线可能会有问题,因为它们可能与C ++实现中的名称冲突。

Cheers & hth., 干杯&hth。,

You should really use vectors as others have suggested. 您应该像其他人建议的那样使用矢量。 A work-around could be as shown (in case you do not want to use memcpy or a loop). 解决方法可以如图所示(如果您不想使用memcpy或循环)。

This would be useful if you have a really huge array. 如果你有一个非常庞大的数组,这将是有用的。 Note that it would add a level of indirection to access the array. 请注意,它将添加一个间接级别来访问该阵列。

class myClass 
{ 
public: 
   myClass(){
      mt = T();    // value initialize.
   }
private:
   struct T{
      int _intArray[10]; 
   } mt;
};

int main(){
   myClass m;
}

you can use another hack basing on a string value and then populate a limited size array check this: https://github.com/Riadam/ViewPort-Array-Shifter-for-Arduino-Uno.git您可以使用基于字符串值的另一个 hack,然后填充一个有限大小的数组检查: https://github.com/Riadam/ViewPort-Array-Shifter-for-Arduino-Uno.git

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

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