c++ - 多重集:多重集添加多个单词的版本并且无法处理大量文本的问题
问题描述
更新并修复:我已经修复了导致错误消息的问题 - 非常感谢用户 PaulMcKenzie 帮助我理解错误消息告诉我的内容! - 当我的程序遇到上面带有标记的字母时(我认为它们是变音符号调用),它崩溃了。我已经调整了我的代码以解决这些问题,现在它根本不会崩溃!另一个非常感谢用户 ihavenoidea 帮助我理解多集!我的程序现在正在按预期的方式工作!
原始帖子:****我对 C++ 非常陌生,因此感谢所有帮助!****
好的,所以我正在尝试使用 multiset 对单词进行排序,这样我就可以看到一个单词在文本中出现了多少次。首先,我的程序接受一个文件,然后它读取单词并取出任何标点符号,然后将它放入一个多重集中。在此之后,应该将结果放入用户自己命名的文本文件中。
我的第一个问题是多重集似乎为同一个单词创建了多个元素(例如:在我的一个测试中,我看到文本文档中列出的 a(4) 连续 3 次而不是一次)。
我的第二个问题是,当我尝试阅读大型文本文档时(我正在使用 John Colliers 的故事“Bottle Party” http://ciscohouston.com/docs/docs/greats/bottle_party.html来测试它)我的程序完全崩溃,但当我用较小的文本文档测试它时不会崩溃(小的说大约 5-10 行文本)。我正在使用 Visual Studios 并且(我也是 Visual Studios 的新手)我不知道错误消息试图告诉我什么,但它说:
代码在这里:
#include <iostream>
#include <string> //for strings
#include <fstream> //for files
#include <set> //for use of multiset
using namespace std;
string cleanUpPunc(string);
//Global variables
multiset <string> words; //will change back to local variable later
int main() {
//Starting variables
string fileName1 = "", fileName2 = "", input = "", input2 = ""; //To hold the input file and the file we wish to print data to if desired
ifstream fileStream; //gets infor from file
//Program start
cout << "Welcome to Bags Program by Rachel Woods!" << endl;
cout << "Please enter the name of the file you wish to input data from: ";
getline(cin, fileName1);
//Trys to open file
try {
fileStream.open(fileName1);
if (!fileStream) {
cerr << "Unable to open file, please check file name and try again." << endl;
system("PAUSE");
exit(1);
}
while (fileStream >> input) {
input2 = cleanUpPunc(input); //sends the input word to check for punctation
words.insert(input2); //puts the 'cleaned up' word into the multiset for counting
}
fileStream.close();
//Sends it to a text document
cout << "Please name the file you would like to put the results into: ";
getline(cin, fileName2);
ofstream toFile; //writes info to a file
//Code to put info into text file
toFile.open(fileName2);
if (toFile.is_open()) {
multiset<string>::iterator pos;
for (pos = words.begin(); pos != words.end(); pos++) {
toFile << *pos << " " << words.count(*pos) << endl;
}
toFile.close();
cout << "Results written to file!" << endl;
}
else {
cout << "Could not create file, please try again." << endl;
}
}catch (exception e) {
cout << "Stop that. ";
cout << e.what();
}
cout << "Thanks for using this program!" << endl;
system("PAUSE");
return 0;
}
string cleanUpPunc(string maybe) {
//Takes out puncuation from string
//Variables
string takeOut = maybe;
//Method
for (int i = 0, len = maybe.size(); i < len; i++) {
if (ispunct(takeOut[i])) {
takeOut.erase(i--, 1);
len = takeOut.size();
}
}
return takeOut;
}
解决方案
推荐阅读
- php - 在数组中搜索最接近 0 的负值和正值
- c++ - 如何让另一个窗口识别 alt+f4 按键
- unix - 如何判断是否是 ssh shell?
- visual-studio-code - Visual Studio Code C/C++ 扩展:是否有 32 位的智能感知模式?
- python - Pandas - 查看 2 列并检查每列是否有不同的元素,如果两列都包含元素,则返回不同列中的值
- python - 对微调的 InceptionResNetV2 进行预测
- javascript - 使用 jquery 获取数据目标并放入 var
- google-cloud-datastore - 带有 ndb 的 GCP 数据存储:使用 ndb.OR 过滤
- jquery - 使用 jquery if 语句控制下拉列表项
- excel - 是否可以在用户窗体上显示打印预览?