Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将文件中的单词放入哈希映射(c+;+;)_C++_Dictionary_File Io_While Loop_Hashmap - Fatal编程技术网

C++ 将文件中的单词放入哈希映射(c+;+;)

C++ 将文件中的单词放入哈希映射(c+;+;),c++,dictionary,file-io,while-loop,hashmap,C++,Dictionary,File Io,While Loop,Hashmap,因此,我有一个相当长的文本文件(10k+个单词),我试图使用标准映射库将每个唯一的单词放入哈希映射 我有一个while循环读取文件中的每个单词。问题是,这个while循环似乎永远不会结束。我甚至在循环中放了一个if语句,这样如果它到达eof(),它就会中断循环。它还没有结束。以下是我迄今为止的代码: #include <iostream> #include <map> #include <string> #include <fstream> #in

因此,我有一个相当长的文本文件(10k+个单词),我试图使用标准映射库将每个唯一的单词放入哈希映射

我有一个while循环读取文件中的每个单词。问题是,这个while循环似乎永远不会结束。我甚至在循环中放了一个if语句,这样如果它到达eof(),它就会中断循环。它还没有结束。以下是我迄今为止的代码:

#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;


string lowerCase(string isUpper);

void main()
{
//create hash map
map<string, int> stringCounts;

//temp string
string nextString;

//import file/write file
ofstream writeFile;
ifstream gooseFile;

//open file to read from
gooseFile.open("goose.txt");
if (gooseFile.is_open()) {
    //read file word by word
    while (gooseFile >> nextString) { //WORKS DO NOT CHANGE
        //check for punctuation
        for (int i = 0; i < nextString.length(); i++) { //WORKS DO NOT CHANGE
            if (nextString[i] == ',' || nextString[i] == '!' || nextString[i] == ';' || nextString[i] == '-' || nextString[i] == '.' || nextString[i] == '?' || nextString[i] == ':' || nextString[i] == '"' || nextString[i] == '(' || nextString[i] == ')' || nextString[i] == '_' || nextString[i] == '\'') {
                nextString.erase(i, i);
                i--;
            }
        }
        //put all into lowercase
        nextString = lowerCase(nextString); //WORKS DO NOT CHANGE
        //cout << nextString << endl;

        //increment key value
        stringCounts[nextString]++;

        if (gooseFile.eof())
            break;
    }
}

//close current file
gooseFile.close();
cout << "I GOT HERE!";
//now print to an output file
writeFile.open("output.txt");
if (writeFile.is_open()) {
    cout << "ITS OPEN AGAIN";
    //write size of map
    writeFile << "The size of the hash map is " << stringCounts.size() << endl;
    //write all words in map
    //create iterator
    map<string, int>::iterator i = stringCounts.begin();
    //iterate through map 
    while (i != stringCounts.end()) {
        writeFile << "The key and value is : (" << i->first << "," << i->second << ")\n";
        i++;
    }
}
else
    cout << "CANT OPEN\n";
}


string lowerCase(string isUpper)
{
    string toReplace = isUpper;
    for (int i = 0; i < toReplace.length(); i++) {
        if (toReplace[i] >= 65 && toReplace[i] <= 90) {
            toReplace[i] = tolower(toReplace[i]);
        }
    }
    return toReplace;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
字符串小写(字符串为大写);
void main()
{
//创建哈希映射
地图计数;
//临时字符串
字符串下一个字符串;
//导入文件/写入文件
流写入文件的类型;
ifstream-gooseFile;
//打开要从中读取的文件
打开(“goose.txt”);
if(gooseFile.is_open()){
//逐字读取文件
而(gooseFile>>nextString){//WORKS不会更改
//检查标点符号
对于(inti=0;istring::erase
(您正在呼叫的)需要一个位置(用于开始擦除的位置)和一个计数(用于擦除的字符数)。因此,此行将删除相当于该字符在字符串中位置的字符数。因此,例如,如果
i
为0,这将删除0个字符。将该事实与下一行合并:

i--;
如果第一个字符是标点符号,
i
将保持在0,for循环将永远不会结束。如果只想擦除1个字符,可以执行以下操作:

nextString.erase(i, 1);
但最好是替换整个for循环,只使用remove/erase习惯用法

auto new_end = std::remove_if(nextString.begin(), nextString.end(),
        [](char c) {
            // return true if c is punctuation
        });
nextString.erase(new_end, nextString.end());

std::map
不是使用哈希表实现的,它通常是通过BST实现的
auto new_end = std::remove_if(nextString.begin(), nextString.end(),
        [](char c) {
            // return true if c is punctuation
        });
nextString.erase(new_end, nextString.end());