C++ 如何填充字符串的可变长度数组

C++ 如何填充字符串的可变长度数组,c++,atmega,C++,Atmega,在javascriptcoffeescript中,我有一些代码如下所示: "BRIGHT": min: 1 max: 4 step: 1 value: 3 bluetooth: options: [ 'off', 'on' ] # SOMETIMES I NEED ARRAY callback: -> rangeFinder.bluetooth = rangeFinder.getSetting().value mode: options: [ 'b

在javascriptcoffeescript中,我有一些代码如下所示:

"BRIGHT":
  min: 1
  max: 4
  step: 1
  value: 3

bluetooth:
  options: [ 'off', 'on' ]  # SOMETIMES I NEED ARRAY
  callback: ->
    rangeFinder.bluetooth = rangeFinder.getSetting().value

mode:
  options: [ 'basic', 'advanced', 'config' ] # OF DIFFERENT LENGTHS
  callback: ->
    rangeFinder.lastMode = rangeFinder.getSetting().value
如何在C++中做这样的事情?< /P> 我有一个由3个类似于亮度的物体组成的数组

#include "setting.cpp"

class GlobalMenu {

  public:

    MenuSetting settings[3];
    int setting;
    GlobalMenu();

};

GlobalMenu::GlobalMenu(void){

  // What is the currently selected setting?
  this -> setting = 0;

  this -> settings[0].name = "BRIGHT";
  this -> settings[0].min = 1;
  this -> settings[0].max = 4;
  this -> settings[0].step = 1;
  this -> settings[0].value = 3;

  this -> settings[1].name = "BLUETOOTH";
  // HOW DO I GET VARIABLE LENGTH ARRAYS HERE?

}
在setting.cpp中

在其他地方,此代码会更改设置并起作用

void RangeFinder::changeSetting(int dir) {

  this -> data.global.settings[this -> data.global.setting].value +=
    (dir ? 1 : -1) *
    this -> data.global.settings[this -> data.global.setting].step;

  this -> enforceMinMax();

  this -> render();
}
另外,如果你能找到一个方法来清理它,这将有所帮助

因此,我可能知道如何检测选项是否有长度,但在将任意数量的选项分配到选项数组中时遇到问题

该解决方案不能使用STD


据我所知,atmega32微控制器无法使用std lib。

为避免您陷入的几个陷阱,您可以选择以下最佳选项:

使用std::string代替字符指针和缓冲区 使用std::vector而不是编译器大小的数组 这将使您的类看起来像:

class MenuSetting 
{
public:
    std::string name;
    int min;
    std::vector<std::string> options;
    int max;
    int step;
    int value;
};

<> p>您可以找到关于C++中的两个和.< /p>< p>的各种操作的详细操作方式,即使用可变大小的数组来使用向量

然后您可以使用options.size来知道有多少元素

在开始使用该语言之前,你也可以从头到尾阅读一篇文章。通过实验,C++不是正确的语言,有几个原因,最重要的是:

有时候C++因为它的历史而不合逻辑,无论你有多聪明,你都无法猜出历史。你需要研究一下。 当您犯错误时,您不能指望语言来帮助您,因为没有运行时错误天使,只有未定义的行为守护进程。
这是不可能的。。。答案是创建case语句和硬代码。最简单的方法是使用指针数组 别忘了删除指针

class MenuSetting {

public:

char *name;
int min;
char* options[5];
int max;
int step;
int value;

};
那么你可以这样分配它

bool MenuSetting::setOption(char *str, unsigned int pos)
{
    if(c && p <5)
    {
        options[pos]=str;
        return true;
    }
    return false;
}
我认为如果您使用智能点,它将对您帮助最大。简单的点并不难实现 我想你可以参考这个网站


此外,我认为这个答案对您也适用

您应该真正使用标准模板库。这将使你的生活更有意义,你将有很多额外的空闲时间来记录你的代码! 使用最新的std库和c++11,可以简化代码,使其看起来像javascript代码片段:

#include <string>
#include <vector>

std::vector<std::string> bluetoothOptions = {"on", "off"};
std::vector<std::string> modeOptions      = {"basic", "advanced", "config"};

// Example usage:
assert (bluetoothOptions[1] == "off");   // exanple to access strings by index
assert (modeOptions.size() == 3);        // example to determine size of vector
如果要强制这些字符串和向量不能更改,请使用以下命令,这是我的首选:

const std::vector<const std::string> bluetoothOptions = {"on", "off"};
const std::vector<const std::string> modeOptions      = {"basic", "advanced", "config"};

我一直在避免使用std,因为当我包含它时找不到它;声明是否要使用std::string.it找不到它可以找到string.h,但std没有string。我不确定它是否与我的MaFe文件有关,我把它附加到了你使用的编译器中。我尝试过使用命名空间STD和String,而不使用命名空间STD和STD::string,甚至组合,但是没有任何工作。问题是用C和C++双重标记的;现在它被修改了,所以根据对的评论中的讨论,它只被标记为C。问题中的代码显然应该是C++类全局菜单,这使得C标签看起来很奇怪。事情很混乱!在我的任何编辑中,它都没有被标记为c。它的C++选项[](5)是一个具有未知长度的选项列表,包含许多长度为5的字符串。所以['on','off']或['yes','no','maybe']您可以为选项创建一个类,而不是char say class options{private:char*strings[/*Some size*/];int numberOfStrings;public:int getNoOfString;char*getStringint index;bool setStringIndex//blah blah}或者更好的东西;
#include <string>
#include <vector>

std::vector<std::string> bluetoothOptions = {"on", "off"};
std::vector<std::string> modeOptions      = {"basic", "advanced", "config"};

// Example usage:
assert (bluetoothOptions[1] == "off");   // exanple to access strings by index
assert (modeOptions.size() == 3);        // example to determine size of vector
const std::vector<const std::string> bluetoothOptions = {"on", "off"};
const std::vector<const std::string> modeOptions      = {"basic", "advanced", "config"};