我曾经想过的一段时间,因为它似乎出现在我的经验丰富的代码相当多。凝聚开关语句?
我有一些使用switch语句的代码很多,但其实际上每次都访问不同的队列。
void store(int toSwitchOn, float posx, float posy){
myDataStruct newValue;
newValue.psX = posx;
newValue.psY = posy;
switch(toSwitchOn){
case 1:
queue1.push(newValue);
break;
case 2:
queue2.push(newValue);
break;
case 3:
queue3.push(newValue);
break;
case 4:
queue4.push(newValue);
break;
case 5:
queue5.push(newValue);
break;
}
}
每个语句中唯一改变的是队列变量。有没有一些巧妙的方法来压缩这种重复的代码?
使用队列数组? – 2012-04-23 20:43:56
从外观上看,你有全局队列。您应该将它们传递给函数。用矢量这很容易;有一个类型为'std :: vector>&'的参数。 –
chris
2012-04-23 20:54:43