我想知道是否有人可以幫助我。我試圖使用列表或結構作爲容器來計算文本文件中字母的頻率,並且在計算解決方案時遇到了一些問題。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