枚举! [英] enum !

查看:87
本文介绍了枚举!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个代码,其中我有一个枚举类似


enum ADDR

{

one = 0;

two = 1;

three = 2;

};

稍后在代码中,我有一个返回ADDR类型的函数,

返回一个指向正确值的整数但是我要打印

out名称一,二或三。

我怎么能这样做,我的例子有很多案例,所以不想使用

开关语句。


谢谢,

Kapil

解决方案



"卡皮尔科斯拉 < KH ********* @ yahoo.com>在留言中写道

news:91 ************************** @ posting.google.c om ...


我有一个代码,其中我有一个枚举类似

枚举ADDR
{
一个= 0;
two = 1;
三= 2;
};

后来在代码中,我有一个返回ADDR类型的函数,它返回一个整数指的是正确的价值,但我想打印出一两个或三个名字。
我怎么能这样做,我的例子有很多案例所以不想使用
切换声明。




AFAIK在你的情况下是不可实现的(带枚举)。

为什么不使用地图?<最好的祝福,

J.Schafer


Kapil Khosla写道:
< blockquote class =post_quotes>
我有一个代码,其中我有一个枚举类似

枚举ADDR
{
一个= 0;
两个= 1;
三个= 2;
};

后来在代码中,我有一个fu返回一个ADDR类型,它返回一个指向正确值的整数,但我想打印出一个,两个或三个名字。
我怎么能这样做,我的例子有很多情况所以不想使用
开关语句。




你可以创建一个字符串表示数组:


const char * ADDR_strings [] = {" one"," two"," three" };


然后你可以这样做:


ADDR addr = get_the_addr();

std: :cout<< ADDR_strings [addr]<< std :: endl;


" Kapil Khosla" < KH ********* @ yahoo.com> schrieb im Newsbeitrag

news:91 ************************** @ posting.google.c om ...


我有一个代码,其中我有一个枚举类似

枚举ADDR
{
一个= 0;
two = 1;
三= 2;
};

后来在代码中,我有一个返回ADDR类型的函数,它返回一个整数指的是正确的价值,但我想打印出一两个或三个名字。
我怎么能这样做,我的例子有很多案例所以不想使用
切换声明。

谢谢,
Kapil




我不知道任何解决方案没有''切换''或者std :: map带插入

为每个枚举器。

在我的应用程序中我使用这样的东西:


std: :ostream的&安培; operator<<(std :: ostream& o,ADDR e)

{

switch(e)

{

案例一:return o<< 一个;

案例二:return o<< 两个;

案例三:return o<< 三;

}

}


格奥尔格


Hi,
I have a code in which I have an enum like

enum ADDR
{
one = 0;
two = 1;
three = 2;
};

Later in code , I have a function that returns an ADDR type, which
returns an integer referring to the correct value but I want to print
out the name one,two or three.
How can I do that, my example has a lot of cases so dont want to use a
switch statement.

Thanks,
Kapil

解决方案


"Kapil Khosla" <kh*********@yahoo.com> wrote in message
news:91**************************@posting.google.c om...

Hi,
I have a code in which I have an enum like

enum ADDR
{
one = 0;
two = 1;
three = 2;
};

Later in code , I have a function that returns an ADDR type, which
returns an integer referring to the correct value but I want to print
out the name one,two or three.
How can I do that, my example has a lot of cases so dont want to use a
switch statement.



AFAIK it''s not achievable in your case (with an enum).
Why not use a map?

With best wishes,
J.Schafer


Kapil Khosla wrote:

Hi,
I have a code in which I have an enum like

enum ADDR
{
one = 0;
two = 1;
three = 2;
};

Later in code , I have a function that returns an ADDR type, which
returns an integer referring to the correct value but I want to print
out the name one,two or three.
How can I do that, my example has a lot of cases so dont want to use a
switch statement.



You can make an array of the string representations:

const char* ADDR_strings[] = { "one", "two", "three" };

Then you can do something like:

ADDR addr = get_the_addr();
std::cout << ADDR_strings[addr] << std::endl;


"Kapil Khosla" <kh*********@yahoo.com> schrieb im Newsbeitrag
news:91**************************@posting.google.c om...

Hi,
I have a code in which I have an enum like

enum ADDR
{
one = 0;
two = 1;
three = 2;
};

Later in code , I have a function that returns an ADDR type, which
returns an integer referring to the correct value but I want to print
out the name one,two or three.
How can I do that, my example has a lot of cases so dont want to use a
switch statement.

Thanks,
Kapil



I dont'' know any solution without a ''switch'' or a std::map with an insert
for each enumerator.
In my applications I use something like this:

std::ostream& operator <<( std::ostream& o, ADDR e )
{
switch( e )
{
case one: return o << "one";
case two: return o << "two";
case three: return o << "three";
}
}

Georg


这篇关于枚举!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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