C++ : 为什么我的程序停止了?迭 代

C++ : Why My Program Stopped ? iterator

本文关键字:为什么 我的 程序 C++      更新时间:2023-10-16

我写了一些代码,我使用迭代器,但我可以在OSX中运行它没有任何问题,但是在Windows中我得到停止错误,我该怎么办? 我附上照片我尝试了大部分编译器(开发C++,Clion,代码块,VS 2012,Cfree)

视窗错误 :

在窗口中停止

OSX :

在OSX中工作正常

这是我的代码:

#include <fstream>
#include <iostream>
#include <sstream>
#include <map>
#include <vector>
#include <list>
#include <string>
#include <conio.h>
#include <stdio.h>
using namespace std;
class Token{
private:
  string word;
  int word_count;
  vector<int> lines;
public:
void setWord(const string &word) {
    Token::word = word;
}
void setWord_count(int word_count) {
    Token::word_count = word_count;
}
void setLines(const vector<int> &lines) {
    Token::lines = lines;
}
Token(const string &word, int word_count, const vector<int> &lines) :     word(word), word_count(word_count),lines(lines) { }
Token(){}
const vector<int> &getLines() const {
    return lines;
}
const string &getWord() const {
    return word;
}
int getWord_count() const {
    return word_count;
}
};
map<string,int> wordCount;
map<string,vector<int>> lineNumbersVector;
list<Token> Alphabets[26];
bool checkWord(std::string word){
bool state = false;
if(isalpha(word.at(0)) && isalpha(word.at(word.length()-1))){
    state = true;
}else{
    state = false;
}
return state;
}
int doSegment(string sentence)
{
std::stringstream ss(sentence);
std::string to;
int lineNumber = 0;
string words[100];
int index = 0;
while(std::getline(ss,to,'n')){
    lineNumber++;
    std::stringstream stream(to);
    std::string word;
    while(std::getline(stream,word,' ')) {
        if(checkWord(word)){
            words[index] = word;
            index++;
            lineNumbersVector[word].push_back(lineNumber);
        }
    }
    for(auto const &ent1 : lineNumbersVector) {
        // ent1.first is the first key
        for(auto const &ent2 : ent1.second) {
            // ent2.first is the second key
            // ent2.second is the data
            //cout << ent2 << "-";
        }
        //cout << "n";
    }
}
for (int i = 0;i<index;i++){
    if (wordCount.count(words[i])>0){
        wordCount[words[i]] = wordCount[words[i]] + 1;
    }else{
        wordCount[words[i]] = 1;
    }
}
std::map<std::string, int>::iterator it = wordCount.begin();
while(it != wordCount.end())
{
    //std::cout<<it->first<<" :: "<<it->second<<std::endl;
    it++;
    Token token;
    switch (it->first.at(0)){
        case 'a':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[0].push_back(token);
            break;
        case 'b':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[1].push_back(token);
            break;
        case 'c':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[2].push_back(token);
            break;
        case 'd':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[3].push_back(token);
            break;
        case 'e':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[4].push_back(token);
            break;
        case 'f':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[5].push_back(token);
            break;
        case 'g':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[6].push_back(token);
            break;
        case 'h':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[7].push_back(token);
            break;
        case 'i':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[8].push_back(token);
            break;
        case 'j':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[9].push_back(token);
            break;
        case 'k':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[10].push_back(token);
            break;
        case 'l':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[11].push_back(token);
            break;
        case 'm':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[12].push_back(token);
            break;
        case 'n':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[13].push_back(token);
            break;
        case 'o':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[14].push_back(token);
            break;
        case 'p':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[15].push_back(token);
            break;
        case 'q':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[16].push_back(token);
            break;
        case 'r':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[17].push_back(token);
            break;
        case 's':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[18].push_back(token);
            break;
        case 't':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[19].push_back(token);
            break;
        case 'u':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[20].push_back(token);
            break;
        case 'v':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[21].push_back(token);
            break;
        case 'w':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[22].push_back(token);
            break;
        case 'x':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[23].push_back(token);
            break;
        case 'y':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[24].push_back(token);
            break;
        case 'z':
            token.setWord(it->first);
            token.setWord_count(it->second);
            token.setLines(lineNumbersVector.at(it->first));
            Alphabets[25].push_back(token);
            break;
    }
}
return 0;
}
Token getWord(list<Token> _list,string word){
std::vector<Token> v{ std::begin(_list), std::end(_list) };
Token foundWord;
cout << v[0].getWord();
my_list.end(), some_value);
for (int i = 0; i < v.size(); ++i) {
}
return foundWord;
}
void searchWord(string wordToSearch) {
cout << "Word:" << wordToSearch << "  Count:" << wordCount.at(wordToSearch)  << "n";
cout << "lines: ";
for (int i = 0; i < lineNumbersVector.at(wordToSearch).size(); ++i) {
    cout << lineNumbersVector.at(wordToSearch)[i] << ",";
}
}
int main() {
std::ifstream ifs("C:program.txt");
std::string content( (std::istreambuf_iterator<char>(ifs) ),
                         (std::istreambuf_iterator<char>()    ) );
doSegment(content);

searchWord("Hi");
}

我该怎么办?多谢

典型的Windows输入文件"C:program.txt"将首先创建一个警告:如果未禁用,则warning: unknown escape sequence: 'p',然后解释为"C:program.txt"。你将逃脱反斜杠。这是在路径中使用反斜杠的典型窗口问题之一。原来的CPM是DOS已经复制的,使用的是/。这是Microsoft为什么将其更改为反斜杠的奥秘之一。