避免嵌套“if's”? [英] Avoid nested "if's"?

查看:63
本文介绍了避免嵌套“if's”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3种类型的对象:bob1,bob2和bob3。每个对象都是由一个唯一ID标识的,该函数由函数getId()返回。


所有bobs都是BaseBob类的后代,它是一个抽象类

有虚函数getId()。


然后我有一个函数在bob遇到时打印一个特殊的字符串

另一个bob(bobs的所有组合都必须满足,因为它们是b
精神分裂症他们也可以满足自己):


void meet_bob(BaseBob b1,BaseBob b2){


if(b1.getType == 1&& b2.getType == 1)

{

cout<< bob1对bob1说嗨 << endl;

}

else if(b1.getType == 1&& b2.getType == 2)

{

cout<< bob1对bob2说嗨 << endl;

}

else if(b1.getType == 1&& b2.getType == 3)

{

cout<< bob1对hi bob3说 << endl;

}

else if(b1.getType == 2&& b2.getType == 2)

{

cout<< bob2对bob2说嗨 << endl;

}

else if(b1.getType == 2&& b2.getType == 3)

{

cout<< bob2对bob3说 <<结束;

}

...

...


}


这不太好看。如果我想使用一个开关,我仍然需要

为外部开关中的每个外壳做一个开关,然后我有一个带嵌套开关的设计

(其中甚至更丑陋。


有没有更好的方法来处理这种情况或者使用嵌套交换机的b $ b是最好的解决方案?

I have 3 types of objects: bob1, bob2 and bob3. Each object is
identified by a unique ID which gets returned by the function getId().

All bobs are descendants from class BaseBob which is an abstract class
that has the virtual function getId().

I then have a function that prints a special string when a bob meets
another bob (all combinations of bobs has to meet and since the are
schizophrenic they can also meet themselves):

void meet_bob(BaseBob b1, BaseBob b2){

if (b1.getType == 1 && b2.getType == 1)
{
cout << "bob1 says hi to bob1" << endl;
}
else if (b1.getType == 1 && b2.getType == 2)
{
cout << "bob1 says hi to bob2" << endl;
}
else if (b1.getType == 1 && b2.getType == 3)
{
cout << "bob1 says hi to bob3" << endl;
}
else if (b1.getType == 2 && b2.getType == 2)
{
cout << "bob2 says hi to bob2" << endl;
}
else if (b1.getType == 2 && b2.getType == 3)
{
cout << "bob2 says hi to bob3" << endl;
}
...
...

}

This is not pretty. If I want to use a switch instead I still have to
make a switch for each case in the outer switch and then I have a design
with nested switches instead (which is even uglier).

Is there some better way to treat this kind of situation or is the use
of nested switches the best solution?

推荐答案

2007年5月10日星期四12:15:15 +0200,desktop写道:
On Thu, 10 May 2007 12:15:15 +0200, desktop wrote:

我有3种类型的对象:bob1,bob2和bob3。每个对象都是由一个唯一ID标识的,该函数由函数getId()返回。


所有bobs都是BaseBob类的后代,它是一个抽象类

具有虚函数getId()。
I have 3 types of objects: bob1, bob2 and bob3. Each object is
identified by a unique ID which gets returned by the function getId().

All bobs are descendants from class BaseBob which is an abstract class
that has the virtual function getId().



嗯... getId()没有出现在你的代码中......

Um... getId() doesn''t appear in your code...


然后我有一个函数,当一个bob遇到

另一个bob时打印一个特殊的字符串(bobs的所有组合必须满足,因为它们是

精神分裂症他们也可以满足自己):


void meet_bob(BaseBob b1,BaseBob b2){
I then have a function that prints a special string when a bob meets
another bob (all combinations of bobs has to meet and since the are
schizophrenic they can also meet themselves):

void meet_bob(BaseBob b1, BaseBob b2){



如果BaseBob是抽象的,你就不能通过值传递它,因为

你不能实例化它。

If BaseBob is abstract, you can''t pass it by value, since
you can''t instantiate it.


if(b1.getType == 1&& b2.getType == 1){

cout<< bob1对bob1说嗨 << endl;

}

else if(b1.getType == 1&& b2.getType == 2){

cout< ;< bob1对bob2说嗨 << endl;

}

else if(b1.getType == 1&& b2.getType == 3){

cout< ;< bob1对hi bob3说 << endl;

}

else if(b1.getType == 2&& b2.getType == 2){

cout< ;< bob2对bob2说嗨 << endl;

}

else if(b1.getType == 2&& b2.getType == 3){

cout< ;< bob2对bob3说 <<结束;

}

...

...


}
if (b1.getType == 1 && b2.getType == 1) {
cout << "bob1 says hi to bob1" << endl;
}
else if (b1.getType == 1 && b2.getType == 2) {
cout << "bob1 says hi to bob2" << endl;
}
else if (b1.getType == 1 && b2.getType == 3) {
cout << "bob1 says hi to bob3" << endl;
}
else if (b1.getType == 2 && b2.getType == 2) {
cout << "bob2 says hi to bob2" << endl;
}
else if (b1.getType == 2 && b2.getType == 3) {
cout << "bob2 says hi to bob3" << endl;
}
...
...

}



我猜你想要什么,但是怎么样:


void meet_bob(BaseBob * pb1,BaseBob * pb2 ){$ / $

cout<< "鲍勃" << pb1-> getId()<< "嗨说鲍勃 << pb2-> getId()<<结束;


...


}


假设一个合理的运算符<<返回类型为getId()。


-

Lionel B

I''m guessing at what you want, but how about something like:

void meet_bob(BaseBob* pb1, BaseBob* pb2){

cout << "bob" << pb1->getId() << " says hi to bob" << pb2->getId() << endl;

...

}

assuming a sensible operator << for the return type of getId().

--
Lionel B


On 10 Maj,12:38,Lionel B< m ... @ privacy.netwrote:
On 10 Maj, 12:38, Lionel B <m...@privacy.netwrote:

On Thu,2007年5月10日12:15:15 +0200,桌面写道:
On Thu, 10 May 2007 12:15:15 +0200, desktop wrote:

我有3种类型的对象:bob1,bob2和bob3。每个对象都是由唯一ID标识的
,该ID由函数getId()返回。
I have 3 types of objects: bob1, bob2 and bob3. Each object is
identified by a unique ID which gets returned by the function getId().


所有bob都是BaseBob类的后代,它是一个抽象类

,它具有虚函数getId()。
All bobs are descendants from class BaseBob which is an abstract class
that has the virtual function getId().



嗯... getId()没有出现在你的代码中......


Um... getId() doesn''t appear in your code...


然后我有一个函数,当一个bob遇到

另一个bob时打印一个特殊的字符串(bobs的所有组合必须满足,因为它们是

精神分裂症他们也可以满足自己):
I then have a function that prints a special string when a bob meets
another bob (all combinations of bobs has to meet and since the are
schizophrenic they can also meet themselves):


void meet_bob(BaseBob b1,BaseBob b2){
void meet_bob(BaseBob b1, BaseBob b2){



如果BaseBob是抽象的,你不能通过价值来传递它,因为

你不能实例化它。


If BaseBob is abstract, you can''t pass it by value, since
you can''t instantiate it.


if(b1。 getType == 1&& b2.getType == 1){

cout<< bob1对bob1说嗨 << endl;

}

else if(b1.getType == 1&& b2.getType == 2){

cout< ;< bob1对bob2说嗨 << endl;

}

else if(b1.getType == 1&& b2.getType == 3){

cout< ;< bob1对hi bob3说 << endl;

}

else if(b1.getType == 2&& b2.getType == 2){

cout< ;< bob2对bob2说嗨 << endl;

}

else if(b1.getType == 2&& b2.getType == 3){

cout< ;< bob2对bob3说 <<结束;

}

...

...
if (b1.getType == 1 && b2.getType == 1) {
cout << "bob1 says hi to bob1" << endl;
}
else if (b1.getType == 1 && b2.getType == 2) {
cout << "bob1 says hi to bob2" << endl;
}
else if (b1.getType == 1 && b2.getType == 3) {
cout << "bob1 says hi to bob3" << endl;
}
else if (b1.getType == 2 && b2.getType == 2) {
cout << "bob2 says hi to bob2" << endl;
}
else if (b1.getType == 2 && b2.getType == 3) {
cout << "bob2 says hi to bob3" << endl;
}
...
...


}
}



我猜你想要什么,但是怎么样:


void meet_bob (BaseBob * pb1,BaseBob * pb2){


I''m guessing at what you want, but how about something like:

void meet_bob(BaseBob* pb1, BaseBob* pb2){



void meet_bob(const BaseBob& b1,const BaseBob& b2){

void meet_bob(const BaseBob& b1, const BaseBob& b2) {


cout<< "鲍勃" << pb1-> getId()<< "嗨说鲍勃 << pb2-> getId()<< endl;
cout << "bob" << pb1->getId() << " says hi to bob" << pb2->getId() <<endl;



cout<< "鲍勃" << b1.getType()<< "嗨说鲍勃 << b2.getType()

<<结束;


-

Erik Wikstr?m

cout << "bob" << b1.getType() << " says hi to bob" << b2.getType()
<< end;

--
Erik Wikstr?m


Lionel B写道:
Lionel B wrote:

On Thu,2007年5月10日12:15:15 +0200,desktop写道:
On Thu, 10 May 2007 12:15:15 +0200, desktop wrote:

>我有3种类型的对象:bob1,bob2和bob3。每个对象都由一个唯一的ID标识,该ID由函数getId()返回。

所有bobs都是BaseBob类的后代,它是一个抽象类
,具有虚拟function getId()。
>I have 3 types of objects: bob1, bob2 and bob3. Each object is
identified by a unique ID which gets returned by the function getId().

All bobs are descendants from class BaseBob which is an abstract class
that has the virtual function getId().



嗯... getId()没有出现在你的代码中......


Um... getId() doesn''t appear in your code...


>然后我有一个函数,当一个bob遇到另一个bob时会打印一个特殊的字符串(bobs的所有组合都必须满足,因为它们也是精神分裂症,他们也可以满足自己):

void meet_bob(BaseBob b1,BaseBob b2){
>I then have a function that prints a special string when a bob meets
another bob (all combinations of bobs has to meet and since the are
schizophrenic they can also meet themselves):

void meet_bob(BaseBob b1, BaseBob b2){



如果BaseBob是抽象的,你不能按值传递它,因为

你不能实例化它。


If BaseBob is abstract, you can''t pass it by value, since
you can''t instantiate it.


> if(b1.getType == 1&& b2.getType == 1){
cout<< bob1对bob1说嗨 << endl;
}
如果(b1.getType == 1&& b2.getType == 2){
cout<< bob1对bob2说嗨 << endl;
}
如果(b1.getType == 1&& b2.getType == 3){
cout<< bob1对hi bob3说 << endl;
}
如果(b1.getType == 2&& b2.getType == 2){
cout<< bob2对bob2说嗨 << endl;
}
如果(b1.getType == 2&& b2.getType == 3){
cout<< bob2对bob3说 << endl;
}
...
...

}
> if (b1.getType == 1 && b2.getType == 1) {
cout << "bob1 says hi to bob1" << endl;
}
else if (b1.getType == 1 && b2.getType == 2) {
cout << "bob1 says hi to bob2" << endl;
}
else if (b1.getType == 1 && b2.getType == 3) {
cout << "bob1 says hi to bob3" << endl;
}
else if (b1.getType == 2 && b2.getType == 2) {
cout << "bob2 says hi to bob2" << endl;
}
else if (b1.getType == 2 && b2.getType == 3) {
cout << "bob2 says hi to bob3" << endl;
}
...
...

}



我是猜测你想要什么,但是怎么样:


void meet_bob(BaseBob * pb1,BaseBob * pb2){


cout< ;< "鲍勃" << pb1-> getId()<< "嗨说鲍勃 << pb2-> getId()<<结束;


...


}


假设一个合理的运算符<<对于返回类型的getId()。


I''m guessing at what you want, but how about something like:

void meet_bob(BaseBob* pb1, BaseBob* pb2){

cout << "bob" << pb1->getId() << " says hi to bob" << pb2->getId() << endl;

...

}

assuming a sensible operator << for the return type of getId().



我更喜欢一般的方法来优化大量的

嵌套& ;如果的"或开关。在每种情况下我都可能想要做一些事情

将来打印字符串会更复杂。


处理大型字符串时是否有嵌套交换机的替代方案

组合数量?我正在考虑一个树形结构,但我确定如果c ++支持这个结构。

Well I was more looking for at general way to optimize a large number of
nested "if''s" or "switches". In each case I might want to do something
more complex that print a string in the future.

Are there alternatives to nested switches when dealing with a large
number of combinations? I was thinking about a tree structure but am nit
sure if that is supported in c++.


这篇关于避免嵌套“if's”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆