枚举数组指数? [英] Enumeration of Array Indicies?

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

问题描述

嘿,


我想为

字母表的每个字母创建一个带有一个索引的数组。好的,够容易的。但是,不是用数组[10]引用字母J

,我可以枚举A为1的每个字母,所以我可以使用
而不是用数组引用J [J]?


我对C ++很陌生,所以请怜悯:P

iw **** @ gmail.com

解决方案

当然......

const char * letters [] =" ABC ... Z";

enum LETTERS {A = 0,B = 1,C = 2,...};


然后


字母[A];

< IW **** @ gmail.com>在消息中写道

news:11 ********************** @ z14g2000cwz.googlegr oups.com ...

嘿,

我想为
字母表的每个字母创建一个带有一个索引的数组。好的,够容易的。但是不是用数组[10]引用字母J
,我可以枚举A为1的每个字母,所以我可以用数组引用J [J]?

我对C ++很陌生,所以请怜悯:P

iw **** @ gmail.com





< IW **** @ gmail.com>在消息中写道

news:11 ********************** @ z14g2000cwz.googlegr oups.com ...

嘿,

我想为
字母表的每个字母创建一个带有一个索引的数组。好的,够容易的。但是不是用数组[10]引用字母J
,我可以枚举A为1的每个字母,所以我可以用数组引用J [J]?

我对C ++很陌生,所以请怜悯:P

iw **** @ gmail.com




你可以(但我不确定你为什么要这么做)。


你可以定义具有特定值的枚举,或者只是从

特定值开始。但请记住,数组中第一项的索引是

0,而不是1.所以,虽然你可以这样做(例如):


enum

{

A = 1,B,C,D,E,F,G,H,I,J

};


....这使你无法使用你的方案索引数组[0]。如果

你使用了字母表中的所有26个字母,请记住你需要声明

" array"作为一个包含27个项目的数组,而不是26个。


但是你的问题让我想知道,这个阵列到底是什么?你说

你想用数组引用J [J],但这是否意味着存在一个

字符J存储在数组中[J],或者你真的是说你想要使用数组[b]引用数组[10]来引用数组[10] ?


另外,你可能想看看使用std :: map,如果你想要的是字符(或字符串)和数据之间的映射



-Howard


IW****@gmail.com 写道:

我想为
字母表的每个字母创建一个带有一个索引的数组。好的,够容易的。但是不是用数组[10]引用字母J
,我可以枚举A为1的每个字母,所以我可以用数组引用J [J]?




''J''如果要将其用作索引,则需要成为名称。正如Martin建议的那样,你可以制作一堆枚举器,从A到任何东西,并且

给它们各自的值。或者,你可以有一堆内容,

喜欢


const int A = 1,B = 2,...


(为什么你想让A等于1而不是0?)


或者你可以写一下


array [''J'']


(假设你在那个数组中有足够的空间)。


V


Hey,

I''d like to make an array with one index for each letter of the
alphabet. OK, easy enough. But instead of referencing the letter J
with array[10], can I enumerate every letter with A being 1 so I could
instead reference J with array[J]?

I''m pretty new to C++ so have mercy :P

iw****@gmail.com

解决方案

certainly...

const char* letters[] = "ABC...Z";
enum LETTERS { A=0, B=1, C=2, ... };

then

letters[A];
<IW****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

Hey,

I''d like to make an array with one index for each letter of the
alphabet. OK, easy enough. But instead of referencing the letter J
with array[10], can I enumerate every letter with A being 1 so I could
instead reference J with array[J]?

I''m pretty new to C++ so have mercy :P

iw****@gmail.com




<IW****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

Hey,

I''d like to make an array with one index for each letter of the
alphabet. OK, easy enough. But instead of referencing the letter J
with array[10], can I enumerate every letter with A being 1 so I could
instead reference J with array[J]?

I''m pretty new to C++ so have mercy :P

iw****@gmail.com



You can (but I''m not sure why you''d want to).

You can define enumerations with specific values, or simply starting at a
specific values. But remember, the index of the first item in an array is
0, not 1. So, while you can do (for example):

enum
{
A = 1, B, C, D, E, F, G, H, I, J
};

....that leaves you with no way to index array[0] using your scheme. And if
you use all 26 letters of the alphabet, remember that you''ll need to declare
"array" as an array of 27 items, not 26.

But your question makes me wonder, exactly what''s in this array? You say
you want to "reference J with array[J]", but does that mean that there is a
character ''J'' stored in array[J], or did you really mean you want to
"reference array[10] using array[J]"?

Also, you might want to look at using std::map, if what you want is a
mapping between characters (or strings) and data.

-Howard


IW****@gmail.com wrote:

I''d like to make an array with one index for each letter of the
alphabet. OK, easy enough. But instead of referencing the letter J
with array[10], can I enumerate every letter with A being 1 so I could
instead reference J with array[J]?



''J'' needs to be a name if you want to use it as an index. As Martin
suggested, you can make a bunch of enumerators, from A to whatever, and
give them respective values. Or, you could have a bunch of contants,
like

const int A = 1, B = 2, ...

(and why do you want to make A equal to 1 and not 0?)

Or you could just write

array[''J'']

(given that you have enough room in that array).

V


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

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