我想知道是否有人能帮助我。我正在尝试使用列表或结构作为容器来计算文本文件中字母的频率,并且在寻找解决方案时遇到了一些问题。
我有一些代码,首先尝试使用
如果可以使用
有谁能帮忙吗?
谢谢。
到目前为止的代码:
我有一些代码,首先尝试使用
struct
,但无法弄清如何轻松地使用指针导航这个对象。有人有添加和遍历struct
条目的示例吗?struct
不是一个可扩展的容器吗?如果可以使用
std::list
,我宁愿这样做,但在阅读std::list
文档后,在此站点或网络上并没有找到足够有用的示例。我需要一个char
变量和一个整数,char
包含发现的字母,整数是我找到每个字母的次数的计数器。有谁能帮忙吗?
谢谢。
到目前为止的代码:
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
struct LetterBox {
char letter;
size_t frequency;
LetterBox* Next;
LetterBox(char ch, size_t count, LetterBox* ptr)
{
letter = ch;
frequency = count;
Next = ptr;
}
};
int main() {
new LetterBox(' ',0,nullptr);
// new LetterBox(' ',0,nullptr);
int count = 0;
char ch;
string line;
string FileName;
cout << "Enter the name of the textfile: ";
cin >> FileName;
ifstream file (FileName);
if (file.is_open())
while (file.good())
{
while(getline(file, line)) {
for (int i = 0; i < sizeof(line); i++) {
ch = toupper(line[i]);
count++;
cout << ch;
}
}
file.close();
}
else
{
cout << "Unable to open file:" << FileName << endl;
return 1;
}
return 0;
}
struct
就是默认公有访问,而不是默认私有访问的class
。你不会说在 C++ 中永远不要使用class
,那么为什么要说永远不要使用struct
呢? - Retired Ninja