【问题标题】:How do I add an enumeration type to reference an array如何添加枚举类型来引用数组
【发布时间】:2011-10-19 16:22:34
【问题描述】:

我的猜测是。但是,我看到的所有示例都创建了 item_type 的实例,比如 item_type_instance。但是我的情况更简单...我想要对我的数组进行描述的东西,而不仅仅是使用 0 和 1。

enum item_type {weight, cost};

然后用重量和成本代替 0 和 1。

void algo(int cost_low,int cost_high,int throw_weight, int item_id)
  {
  int quantity,remainder;
  quantity=throw_weight/item_matrix[item_id][0];
  remainder=throw_weight%item_matrix[item_id][0];
  if(remainder==0)
    {
    cost_low=(quantity-1)*item_matrix[item_id][1];
    cost_high=quantity*item_matrix[item_id][1];
    throw_weight-=(quantity-1)*item_matrix[item_id][0];
    }
  else
    {
    cost_low=quantity*item_matrix[item_id][1];
    cost_high=(quantity+1)*item_matrix[item_id][1];  
    throw_weight-=quantity*item_matrix[item_id][0];
    }
  }

【问题讨论】:

  • 顺便说一句,这个算法没有副作用,也没有返回值……所以它相当于void foo(){}。除非您打算通过 algo( int& cost_low etc...) 中的引用传递 cost_lowcost_highthrow_weight
  • 是的,我想传递作为 cost_low、cost_high 和 throw_weight 的参考...需要更新

标签: c++


【解决方案1】:

你当然可以这样做;但是您难道不想用比数组更有意义的东西来表示item_matrix 中的items 吗?

struct Item {
  int weight;
  int cost;
};

这可能会使您的算法更具可读性:

void algo(int cost_low,int cost_high,int throw_weight, int item_id)
  {
  int quantity,remainder;
  Item& item = item_matrix[item_id];
  quantity=throw_weight/item.weight;
  remainder=throw_weight%item.weight;
  if(remainder==0)
    {
    cost_low=(quantity-1)*item.cost;
    cost_high=quantity*item.cost;
    throw_weight-=(quantity-1)*item.weight;
    }
  else
    {
    cost_low=quantity*item.cost;
    cost_high=(quantity+1)*item.cost;  
    throw_weight-=quantity*item.cost;
    }
  }

也许可以进一步重构,并将计算也委托给Item

-- 编辑 我无法抗拒...可以委托给 Item 本身,摆脱所有 item.xxx 符号。

struct Item {
   int weight;
   int cost;

   void algo( int& cost_low, int& cost_high, int& throw_weight ) {
      int quantity = throw_weight / weight;
      int remainder = throw_weight % weight;

      cost_low=(quantity-1)*cost;
      cost_high=quantity*cost;
      throw_weight -= (quantity-1)*weight;

      if( remainder != 0 ) {
         cost_low += cost;
         cost_high += cost;
         throw_weight += weight;
      }
   }
};

用法:

item_matrix[item_id].algo( cost_low, cost_high, throw_weight );

【讨论】:

  • 但是有了一个数组,我知道内存是如何布局的......连续......但对于一个结构我不......
  • 你也可以查一下,也发现了。
  • @user1001776,你对内存连续布局有要求吗?结构的布局由标准很好地定义,除了可能的填充,当结构中的项目是相同类型时这不应该成为问题。
  • @user1001776: "google c++ struct memory-layout",指向stackoverflow.com/questions/422830/…
【解决方案2】:

只需将枚举数定义为 0 和 1:

enum item_type
{
    weight = 0
  , cost = 1
};

enumint 的标准转换将允许您使用枚举来索引数组。

【讨论】:

  • 0 和 1 是默认值..正确..我不必明确地这样做
  • 是的,0 和 1 是默认值,但您希望明确说明。否则有人可能会稍后来交换枚举数的顺序,您的代码将停止工作。
  • 如果您依赖于 weightcost 的值分别为 0 和 1,请按照 @K-ballo 演示并明确指定值,作为对未来代码维护者的信号他们需要拥有这些价值观。它阻止人们在枚举的前面(或它们之间)插入新项目,从而破坏您的代码。
【解决方案3】:

如果我正确理解您的问题,那么我认为您想使用 enum 作为数组的索引。如果是这样,那么您可以这样做:

quantity=throw_weight/item_matrix[item_id][weight]; //weight <=> 0
cost_low=(quantity-1)*item_matrix[item_id][cost];   //cost   <=> 1

weightcost的值分别是01,所以上面的代码完全没问题。如果未提供枚举值,则它以0 开头,并随着每个后续枚举标签递增1

【讨论】:

  • @user1001776:是的。你为什么不试试?
  • 我知道有人会这样说...b.c 我刚刚重构了大约 600 行代码...并且需要一段时间来挖掘思想错误...但是是的,我应该逐步进行更改并随着我的前进而修复……不耐烦了……但是是的,您是对的
猜你喜欢
  • 1970-01-01
  • 2011-04-27
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
相关资源
最近更新 更多