【问题标题】:How to set a group of constant values based on an initial variable set by the user如何根据用户设置的初始变量设置一组常量值
【发布时间】:2017-02-17 12:08:25
【问题描述】:

我的程序的输出是一个数组,这个数组的大小是基于用户输入的。 但是要设置数组的大小,我需要常量。

所以一种解决方案是让用户在编译/运行之前设置常量。

const int test1 = 10;
const int test2 = 20;
std::string TestArray[test1][test2];

然而,除了 Array 的 2 个常量之外,还有几个常量需要设置,因此理想情况下,用户只需设置 1 个变量,然后使用如下开关根据该变量设置常量:

const int number = 2;
int test1a;
int test2a;
switch (number)
{
case 1:
    test1a = 10;
    test2a = 10;
    test3a = 123;
    break;
case 2:
    test1a = 20;
    test2a = 20;
    test3a = 456;
    break;
}
const int test1 = test1a;
const int test2 = test2a;
std::string TestArray[test1][test2];
test2 = 50;

但是,这会导致 test1 和 test 2 在设置数组时出现“表达式必须具有常量值”的错误。但是尝试设置 test2 = 50 之后的行给出了错误“表达式必须是可修改的 Ivalue”

正在设置的数据是建筑信息。 因此,第 1 组将针对具有 x 层、y 人等的普通办公楼 第 2 组平均酒店 第三组平均住宅区 等等

【问题讨论】:

  • 可以使用 ternar 运算符: const int test1 = ( (number==1) ? 10 : 20 );
  • 我可以用“number==variable”代替“number==1”吗?然后我可以设置一个变量来设置多个常量。
  • 如果变量是 const 应该没问题

标签: c++ arrays constants


【解决方案1】:

你可以使用模板,比如:

template <std::size_t> struct config;

template <> struct config<1>
{
    static constexpr int test1a = 10;
    static constexpr int test2a = 10;
    static constexpr int test3a = 123;
};

template <> struct config<2>
{
    static constexpr int test1a = 20;
    static constexpr int test2a = 20;
    static constexpr int test3a = 456;
};

constexpr std::size_t number = 2;
const int test1 = config<number>::test1a;
const int test2 = config<number>::test2a;

【讨论】:

  • 非常感谢!你的“config”和 WhiZTiM 的“Switch{}”有什么区别? "switch 看起来是一个实际的函数。"config" 只是一个指针吗?
  • @SamDean: config 是一个类模板。这里没有指针。和 WhiZTiM 的解决方案一模一样。
【解决方案2】:

您不能在函数之外使用 switch,而且无论如何您使用了错误的解决方案。您的问题的解决方案是创建一个动态数组,尝试用谷歌搜索它,然后询问您是否有任何问题。

编辑:

   #define number 2
   #if number == 2
   const int test2 = 10
   #else
   const int test2 = 20
   #endif

【讨论】:

  • 非常感谢,快速浏览后我认为这将解决我的数组问题,但首先我将尝试使用模板,因为它们也解决了其他常量。谢谢!
【解决方案3】:

因此,一种解决方案是让用户在使用之前设置常量 编译/运行。

数组大小必须在编译时知道。但是,您可以使用编译时开关(使用 class-templates):

完整示例:

#include <string>

template<int>
struct Switch{};

template<>
struct Switch<1>{
    static constexpr int test1a = 10;
    static constexpr int test2a = 10;
    static constexpr int test3a = 123;
};

template<>
struct Switch<2>{
    static constexpr int test1a = 20;
    static constexpr int test2a = 20;
    static constexpr int test3a = 456;
};

int main(){
    constexpr int number = 2;    //Change to 1 if you require the other.
    constexpr int test1 = Switch<number>::test1a;
    constexpr int test2 = Switch<number>::test1a;

    std::string TestArray[test1][test2];
}

如所见Live On Coliru

【讨论】:

    【解决方案4】:

    不要使用固定大小的对象数组,而是尝试使用指向对象数组或指针数组的指针,然后您可以根据用户的输入分配内存。

    int number={0};
    cin >> number;
    int* array{new int{number}};
    

    但是您的代码的问题在于它更像是 C 风格的编程。这可以更容易地使用 std::vector 或使用类来完成。

    【讨论】:

    • 谢谢!我来看看 std::vectors
    • 您引入了手动内存管理和内存泄漏。不好。
    • 别自作聪明。我根据他的问题写了代码,我告诉他C风格的代码不好。下次尝试回答问题并帮助某人。
    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多