C++ C++;:为什么我的程序停止了?迭代器
我写了一些代码,我使用迭代器,但我可以在OSX中运行它没有任何问题,但在windows中,我得到停止错误,我该怎么办?我附上照片 我尝试了大多数编译器(DEV C++,克利永,CODBOLKID,VS 2012,CFEL)< /P> Windows错误: OSX: 这是我的密码:C++ C++;:为什么我的程序停止了?迭代器,c++,macos,iterator,C++,Macos,Iterator,我写了一些代码,我使用迭代器,但我可以在OSX中运行它没有任何问题,但在windows中,我得到停止错误,我该怎么办?我附上照片 我尝试了大多数编译器(DEV C++,克利永,CODBOLKID,VS 2012,CFEL)< /P> Windows错误: OSX: 这是我的密码: #include <fstream> #include <iostream> #include <sstream> #include <map> #include &
#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");
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类令牌{
私人:
字符串字;
整数字数;
矢量线;
公众:
无效设置字(常量字符串和字){
令牌::单词=单词;
}
无效设置字计数(整数字计数){
令牌::单词计数=单词计数;
}
无效设置线(常量向量和线){
令牌::行=行;
}
标记(常量字符串和单词、整数单词计数、常量向量和行):单词(单词)、单词计数(单词计数)、行(行){
令牌(){}
常量向量&getLines()常量{
回流线;
}
常量字符串&getWord()常量{
返回词;
}
int getWord_count()常量{
返回字数;
}
};
映射字数;
地图行号向量;
列出字母表[26];
布尔校验字(标准::字符串字){
布尔状态=假;
if(isalpha(word.at(0))&&isalpha(word.at(word.length()-1))){
状态=真;
}否则{
状态=假;
}
返回状态;
}
int-doSegment(字符串句)
{
std::stringstream ss(句子);
std::字符串到;
int lineNumber=0;
字符串[100];
int指数=0;
while(std::getline(ss,to,'\n')){
lineNumber++;
std::stringstream(to);
字符串字;
while(std::getline(流,字,')){
if(校验字(word)){
单词[索引]=单词;
索引++;
lineNumbersVector[word]。推回(lineNumber);
}
}
用于(自动常量和ent1:lineNumbersVector){
//ent1.first是第一个键
for(自动常量和ent2:ent1.秒){
//ent2.first是第二个键
//其次是数据
//库特(秒);
setLines(lineNumbersVector.at(it->first));
字母[2]。推回(标记);
打破
案例“d”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[3]。推回(标记);
打破
案例“e”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[4]。推回(标记);
打破
案例“f”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[5]。推回(标记);
打破
案例“g”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[6]。推回(标记);
打破
案例“h”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[7]。推回(标记);
打破
案例“i”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[8]。推回(标记);
打破
案例“j”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[9]。推回(标记);
打破
案例“k”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[10]。推回(标记);
打破
案例“l”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[11]。推回(标记);
打破
案例“m”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[12]。推回(标记);
打破
案例“n”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[13]。推回(标记);
打破
案例“o”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[14]。推回(标记);
打破
案例“p”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[15]。推回(标记);
打破
案例‘q’:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[16]。推回(标记);
打破
案例“r”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[17]。推回(标记);
打破
案例s:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[18]。推回(标记);
打破
案例“t”:
token.setWord(it->first);
token.setWord\u count(它->秒);
setLines(lineNumbersVector.at(it->first));
字母[19]。推回(标记);
打破
案例“u”:
token.setWord(it->first);
token.setWord\u计数