我想知道是否有人可以帮助我。我试图使用列表或结构作为容器来计算文本文件中字母的频率,并且在计算解决方案时遇到了一些问题。C++ - 没有匹配的构造函数
下面我有一些代码,并首次尝试使用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;
}
你应该使用地图:http://www.cplusplus.com/reference/map/map/。一般来说,你不应该在C++中使用'struct'。 – DyZ
为什么不使用std :: map直接关联字符和它的计数器? –
您的'LetterBox'结构可以包含Array或List作为包含类。或者你可以使用链表。 – MKR