如何从字符中检索整数值?
如何将字符转换为整数值?例如,我一直尝试将“A”读作1,将“B”读作2,依此类推。我尝试将字符与字母表中的每个字母进行比较并返回适当的值。
int intvalue(char letter)
{
if(letter == "A")
return 1;
else if(letter == "B")
return 2;
else if(letter == "C")
return 3;
else if(letter == "D")
return 4;
else if(letter == "E")
return 5;
else if(letter == "F")
return 6;
else if(letter == "G")
return 7;
else if(letter == "H")
return 8;
else if(letter == "I")
return 9;
else if(letter == "J")
return 10;
else if(letter == "K")
return 11;
else if(letter == "L")
return 12;
else if(letter == "M")
return 13;
else if(letter == "N")
return 14;
else if(letter == "O")
return 15;
else if(letter == "P")
return 16;
else if(letter == "Q")
return 17;
else if(letter == "R")
return 18;
else if(letter == "S")
return 19;
else if(letter == "T")
return 20;
else if(letter == "U")
return 21;
else if(letter == "V")
return 22;
else if(letter == "W")
return 23;
else if(letter == "X")
return 24;
else if(letter == "Y")
return 25;
else if(letter == "Z")
return 26;
我
收到“错误:ISO C++ 禁止指针和整数之间的比较”。有谁知道如何解决这个问题?或者更好的是,采取不同的方式来解决这个问题?我觉得我的上述功能非常暴力。
How can I convert a character into an integer value? For example, I've been trying to read "A" as 1, "B" as 2, and so forth. I tried comparing the character to each letter in the alphabet and return the appropriate value.
int intvalue(char letter)
{
if(letter == "A")
return 1;
else if(letter == "B")
return 2;
else if(letter == "C")
return 3;
else if(letter == "D")
return 4;
else if(letter == "E")
return 5;
else if(letter == "F")
return 6;
else if(letter == "G")
return 7;
else if(letter == "H")
return 8;
else if(letter == "I")
return 9;
else if(letter == "J")
return 10;
else if(letter == "K")
return 11;
else if(letter == "L")
return 12;
else if(letter == "M")
return 13;
else if(letter == "N")
return 14;
else if(letter == "O")
return 15;
else if(letter == "P")
return 16;
else if(letter == "Q")
return 17;
else if(letter == "R")
return 18;
else if(letter == "S")
return 19;
else if(letter == "T")
return 20;
else if(letter == "U")
return 21;
else if(letter == "V")
return 22;
else if(letter == "W")
return 23;
else if(letter == "X")
return 24;
else if(letter == "Y")
return 25;
else if(letter == "Z")
return 26;
}
I got "error: ISO C++ forbids comparison between pointer and integer". Does anyone know how to fix this? Or even better, a different way to go about this? I feel like my above function is very brute-forceish.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
更可移植的实现涉及搜索字符数组并使用索引作为值:
此函数也适用于 UTF 格式。此方法不对整理顺序做出任何假设。
A more portable implementation involves searching an array of characters and using the index as your value:
This function will work with UTF formats as well. This method makes no assumptions about the collating sequence.
一种可能的方法是使用从
char
到int
的显式转换:输入:
输出:
A possible approach is just using an explicit cast from
char
toint
:Input:
Output:
您需要使用字符文字,而不是字符串文字,例如,
也就是说,如果您愿意假设您正在使用 ASCII 字符集的系统上运行,则可以简单地使用算术:
在 ASCII 字符集中,字母是连续的索引,所以这有效。这可能不适用于其他字符集。
You need to use character literals, not string literals, e.g.,
That said, if you are willing to assume you are running on a system using the ASCII character set, you can simply use arithmetic:
In the ASCII character set, letters are at consecutive indices, so this works. This may not work with other character sets.
在 C++ 中,
char
是一个数字。因此不需要像上面那样复杂的方法:您可以直接使用char
。如果你想让它基于 A=1 如上所示,那么你可以这样做:至于你得到的错误,在 C/C++ 中,双引号字符串是
char*
(或者,更准确地说,是char[]
,但这里的区别并不重要)。这就是您收到错误的原因:您正在尝试比较char
和char*
。In C++, a
char
is a number. So there's no need for an elaborate method like above: you can just use thechar
directly. If you want to make it based with A=1 as shown above, then you can do:As for the error you're getting, in C/C++, a double-quoted string is a
char*
(or, more accurately, achar[]
, but the difference isn't important here). That's why you're getting an error: you're trying to compare between achar
and achar*
.试试这个:(
尽管您可能想要处理超出范围的
letter
输入)请注意,
char
是单个字符,它使用单引号:' A'
。字符串是多个字符并使用双引号:"ABC"
。您可以将字符视为整数(它们的 ascii 值)并对其进行操作。
Try this:
(Although you may want to handle an out-of-range
letter
input)Note that
char
is a single character, which uses single quotes:'A'
. Strings are multiple characters and use double quotes:"ABC"
.You can treat characters as integers (their ascii values) and manipulate them.
字符及其 ASCII 值整数是可以互换的。字符的 ASCII 值减去
'A'
的 ASCII 值将为 0,因此加 1 会得到 1:从技术上讲,您也可以只减去
'@'
(前面的字符)表中的“A”),但我认为这可能不太清楚。您的问题是您使用的是"A"
而不是'A'
;前者是一个字符串(技术上是指向字符的指针),而后者是单个字符Characters and their ASCII value integers are interchangeable. The character's ASCII value minus
'A'
s ASCII value would be 0, so adding 1 would give you 1:Technically you could also just subtract
'@'
(the character before 'A' in the table), but I think it's probably less clear. Your problem is you were using"A"
instead of'A'
; the former is a string (technically a pointer to a character), while the latter is a single character