c++:新的多维字符串数组

标签 c++

<分区>

Possible Duplicate:
C++ multi dimensional array

我正在尝试创建一个类,该类根据对象构造函数参数之一创建任意大小的字符串数组。

这里是我目前正在尝试的对象构造函数的代码:

commandSpec::commandSpec(int numberOfCommands)
{
    std::string * commands = new std::string[3][numberOfCommands];
}

我得到一个错误:“numberOfCommands 不能出现在常量表达式中”,有人可以告诉我在执行之前我不知道大小的对象中指定数组的正确方法。

谢谢你

最佳答案

这可能应该作为一个结构体和一个 vector 来实现,如下所示:

struct command {
    std::string first;
    std::string second;
    std::string third;
};

commandSpec::commandSpec(int numberOfCommands)
{
    std::vector<command> commands(numberOfCommands);
}

当然,你应该为command的成员选择合适的名字。

关于c++:新的多维字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6292553/

相关文章:

C++ 访问说明符

c++ - 为什么 auto 关键字不能与指向函数的指针的初始化列表一起使用?

c# - ActiveX 的 .NET 使用者抛出 TargetParameterCountException

c++ - 控制 map 中元素的顺序

c++ - nlohmann json 库将数组转换为结构 vector

c++ - 如何在不损失性能的情况下用一个进程写入一个 vector 并用另一个进程读取它

c++ - 函数中静态变量的生命周期

c++ - 使用 cin 读取带有任意空格的逗号分隔整数对 (x,y)

c++ - 静态常量匿名 union 使用 GCC 4.9.3 给出 "uninitialized const"错误

c++ - 如何确保 Windows 2008 R2 将 SERVICE_CONTROL_PRESHUTDOWN 控制通知传递给服务