这段代码真的让我感到困惑,它使用了一些斯坦福库来创建 Vector(数组)类。谁能告诉我 int index = line [j] - 'a';
why - 'a' 的目的是什么?
void countLetters(string filename)
{
Vector<int> result;
ifstream in2;
in2.open(filename.c_str());
if (in.fail()) Error("Couldn't read '" + filename + "'");
for (int i = 0; i < ALPHABETH_SIZE; i++)
{
result.add(0); // Must initialize contents of array
}
string line;
while (true)
{
getLine(in, line);
// Check that we got a line
if (in.fail()) break;
line = ConvertToLowerCase(line);
for (int j = 0; j < line.length(); j++)
{
int index = line [j] - 'a';
if (index >= 0 && index < ALPHABETH_SIZE)
{
int prevTotal = result[index];
result[index] = prevTotal +1;
}
}
}
}
代码的用途:
获取文件名并打印字母表中每个字母在该文件中出现的次数。因为要打印26个数字,CountLetters需要创建一个Vector。例如,如果文件是:
最佳答案
字符串中的字符使用字符集编码...通常是英语语言系统中常见的硬件上的 ASCII。您可以在 http://en.wikipedia.org/wiki/ASCII 查看 ASCII 表
在 ASCII(和大多数其他字符集)中,代表字母的数字是连续的。因此,这是测试字符数组 line
中索引 j
处的字符是否为字母的自然方法:
line[j] >= 'a' && line[j] <= 'z'
你的程序等同于,在代数意义上,它从两边减去 a
(知道 a
是字符集中的第一个字符) :
line[j] >= 'a' - `a` && line[j] <= 'z' - `a`
line[j] >= 0 && line[j] <= 'z' - `a`
将“<= z
- a
”替换为等效的:
line[j] >= 0 && line[j] < ALPHABET_SIZE
其中 ALPHABET_SIZE 是 26。这需要知道 z
是字符集中的最后一个字符,才能知道字符集中有多少个字符 - 两者都有点脆弱,但如果你知道你正在处理一个众所周知的、稳定的字符集编码。
检查字母的更好方法是使用 isalpha()
谓词:http://www.cplusplus.com/reference/clibrary/cctype/isalpha/
关于c++ - C++ 字符串的算术运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13302429/