简体   繁体   English

C ++令牌化字符串不可取消引用

[英]C++ Tokenize string not dereferencable

I am getting a "string not dereferencable" error for my code, which is pretty much copied verbatim from somewhere on the internet. 我的代码出现“字符串不可引用”错误,该错误几乎是从互联网上的某处逐字复制的。 The application compiles perfectly in release mode (VS 2010), however keeps throwing me an error in Debug mode. 该应用程序在发行模式(VS 2010)下可以完美地编译,但是在调试模式下总是使我抛出错误。 It should be splitting the string at the * and saving each of the words to a vector. 应该在*处分割字符串,并将每个单词保存到向量中。 Does anyone have any ideas? 有人有什么想法吗? It really doesn't seem to like the (string::npos != found) part of the comparison. 它似乎确实不喜欢比较的(string :: npos!=找到)部分。

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);
}

Your help is gratefully appreciated!!! 非常感谢您的帮助!!!

Your code didn't produce any errors for me in VS2010. 您的代码在VS2010中对我没有产生任何错误。

Since you've got access to regular expressions ( <regex> library), another alternative could be: 由于您可以访问正则表达式( <regex>库),因此另一种选择可能是:

std::string str = "Something*NotCool";
std::regex re("[^(\\*\\+%/\\-\\(\\))]+");
std::sregex_token_iterator begin(str.begin(), str.end(), re), end;
std::vector<std::string> tokens;
std::copy(begin, end, std::back_inserter(tokens));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM