我的程序没有一点崩溃,工作正常,就像我想要的那样:
#include <iostream>
#include <string>
#include <fstream>
#include <map>
typedef std::map<int,std::string> strlist;
strlist get_program(std::string RELPATH)
{
std::ifstream file(RELPATH);
strlist out;
for(int i = 0; i < 3; ++i)
{
std::getline(file,out[i+1]);
}
return out;
}
int main()
{
strlist program=get_program("program.txt");
strlist tokenlist=get_tokens(program[1],' '); //in the "version of the program that doesn't crash",
//this line is removed due to the functions it is directly and indirectly referring to being removed
std::string in,out;
std::cout<<"[Cyan 0.1]\n";
std::cin>>in;
for(int i=1;i<=program.size();i++)
{
std::cout<<program[i]<<"\n";
}
std::cin>>in;
return 0;
}
程序中存在的部分以某种方式导致崩溃(它在主函数之前放置):
bool checkforchars(std::string chars,char c) //Returns true if one of char's in given string is identical to given char c
{
for(int i=0; i<chars.length();i++)
{
if(chars[i]==c) {return true;}
}
return false;
}
strlist get_tokens(std::string in,char tokenCut)
{ //Returns an ordered (by occurence) list of strings with each string containing a single token. The separation of tokens is decided by the single character specified by tokenCut
strlist tokens;
int token_amnt=1;
for(int i=0;i<in.length();i++)
{
if(in[i]==tokenCut)
{
token_amnt++;
}
else
{
tokens[token_amnt]=tokens[token_amnt]+in[i];
}
}
}
我正在翻译(我称之为#34; Cyan&#34;)以及没有&#34;崩溃&#34; bit工作正常并且在我给它一些输入后打印program.txt的行(第一个输入实际上不会影响除下一位之外的任何事情,直到用户输入为止)。如果我添加&#34;崩溃&#34;与行&#34; strlist tokenlist = get_tokens(program [1],&#39;&#39;);&#34;它崩溃了,但有两个奇怪的事情:
如果它涉及&#34;崩溃功能&#34;虽然我仍然对使其崩溃的原因同样无能为力,但它会更有意义。我正在使用MinGW for Windows 7(32位)并使用
进行编译g++ main.cpp -std=c++11 -o program.exe
如果这有任何帮助。