首页 > 解决方案 > 多重集:多重集添加多个单词的版本并且无法处理大量文本的问题

问题描述

更新并修复:我已经修复了导致错误消息的问题 - 非常感谢用户 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 的新手)我不知道错误消息试图告诉我什么,但它说: 错误信息

选择重试后: 错误2 一如既往,非常感谢任何和所有帮助。

代码在这里:

#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;
}

标签: c++multiset

解决方案


推荐阅读