請問您會告訴我什麼是在所有字符串中查找字母表的過程(字符串列表)在C++中 輸出將只返回匹配的值(字母數字) 我嘗試這個程序檢查是否有任何字母表(az)存在於所有字符串(小說列表)中並且打印發現的字母數量
#include <vector>
#include <iostream>
#include <cstdio>
int main()
{
//setup
std::vector<int> alphabetCount;
for (int i = 0; i < 26; ++i)
{
alphabetCount.push_back(0);
}
//now the interactive bit
std::cout << "Enter a line of text\n";
std::string line;
std::getline(std::cin, line);
for (size_t i = 0; i < line.size(); ++i)
{
char currentChar = tolower(line[i]);
if (isalpha(currentChar))
{
++alphabetCount[currentChar - 'a']; //subtract a, so if currentChar = a, 'a' - 'a' = 0, so its index 0
}
}
for (size_t i = 0; i < alphabetCount.size(); ++i)
{
std::cout << "there were " << alphabetCount[i] << " occurences of " << static_cast<char>(i + 'a') << "\n"; //add 'a' for the same reason as above, though we have to cast it to a char.
}
system("pause");
return 0;
}
但只返回一個字符串,我想爲什麼它正在一個從所有的字符串
題外話:'的std ::矢量 alphabetCount(26);'將與26個零預加載向量和替換第一個for循環。 –
user4581301