我得到一个“字符串不可忽略”的错误代码,它几乎是从互联网上某处复制的。应用程序在发布模式下完美编译(VS 2010),但在调试模式下不断抛出错误。它应该将字符串拆分为*并将每个单词保存为一个向量。有没有人有任何想法?它似乎并不喜欢比较中的(string :: npos!= found)部分。C++ Tokenize字符串不可忽略
string newString = "Something*NotCool";
size_t found = newString.find_first_of("+*-/%()");
size_t lastPos = 0;
//while (found != newString.length)
while (string::npos != found || string::npos != lastPos)
{
if (found >= newString.length()) break;
if (found == lastPos)
{
lastPos = found+1;
found = newString.find_first_of("+*-/()", found+1);
}
string temp (newString,lastPos,found);
temp.assign(newString, lastPos, found-lastPos);
strings.push_back(temp);
lastPos = found+1;
found = newString.find_first_of("+*-/()", found + 1);
}
您的帮助表示感谢!
它编译我使用VS 2010的(我用的CL,而不是IDE)。你将需要显示更多的代码。 – 2012-04-17 22:51:30
你确定这是引发错误的代码吗?我怀疑这是添加了一些东西来避免错误信息的代码,但是它仍然存在最后一个字不会添加到'strings'的问题。另外,请注意编译器错误和运行时错误之间的区别 - 它们完全不同,并且说'完美编译'与说它运行没有错误消息(这与说正确运行不同)不同。 – 2012-04-17 22:56:38
我想你的'if(found == lastPos)'逻辑是关闭的。这似乎试图检测一行中有两个* s,并忽略它们。但是,连续3个或更多*不会被捕获。 – Lalaland 2012-04-17 22:58:02