C++ : 为什么我的程序停止了?迭 代
C++ : Why My Program Stopped ? iterator
我写了一些代码,我使用迭代器,但我可以在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为什么将其更改为反斜杠的奥秘之一。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么我的代码在输出中增加了93天
- 为什么我的for循环不能正确获取argv
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 为什么我的 std::ref 无法按预期工作?
- 为什么我的删除节点函数实际上没有删除节点?
- 为什么我的 IExtractIcon 处理程序没有被调用?
- 为什么我的多线程作业队列崩溃
- 为什么我的排序算法会更改数组值
- 为什么我的变量没有更新,我的 LED 没有亮起?
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 为什么我的共享库中存在展开符号
- 为什么我的C++代码中出现'Segmentation Fault: 11'行?
- 为什么我的递归函数按降序打印,然后按升序打印?
- 为什么我的 heap.h 文件给我一个LNK2001错误?
- 为什么我的好友类无法访问私人会员?
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 为什么我的模板化函数需要从一个迭代器转换到另一个迭代器?