0

コードで「string not dereferencable」エラーが発生します。これは、インターネット上のどこかから逐語的にコピーされたものです。アプリケーションはリリース モード (VS 2010) で完全にコンパイルされますが、デバッグ モードではエラーが発生し続けます。* で文字列を分割し、各単語をベクトルに保存する必要があります。誰にもアイデアはありますか?比較の (string::npos != found) 部分が本当に気に入らないようです。

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

あなたの助けに感謝します!!!

4

1 に答える 1

1

あなたのコードは、VS2010 でエラーを生成しませんでした。

正規表現(<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));
于 2012-04-17T23:03:51.657 に答える