0

確かに、C++ に関して言えば、文字列操作が私の長所ではなかったことは認めます。Boost を使用するだけでさまざまなケースでかなりの成功を収めましたが、まだいくつかの問題があります。

だから、ここに私が必要なものがあります(例は正確です)...


入力は次のとおりです。

position fen <arg_a1> <arg_a2> ... <arg_aN> moves <arg_b1> <arg_b2> ... <arg_bN>

(まあ、このトピックに精通している人にとっては、これは UCI プロトコルのコマンドです)

私がやりたいことは次のとおりです。

  • get <arg_a1> <arg_a2> ... <arg_aN>(スペースを含む) を 1 つの文字列 (つまり、 と の間の文字列) にしますpositionmoves
  • また、<arg_b1> <arg_b2> ... <arg_bN>別の文字列 (つまりmoves、最後までの文字列) に入ります。

私はたくさん遊んできましたがBoost::split、明らかにこれは(文字列区切り文字ではなく文字を受け入れます)のようstrtokに機能しています。

どうすればいいですか?車輪を再発明することなく、この特定の問題を解決する最も簡単な方法は何ですか?

ヒント :もちろん、私はそれを行うことができます。しかし、私が今考えることができるのは、かなり醜いことだけです。私が探しているのは、C++ に適した方法です...

4

1 に答える 1

0

そして、これが解決策です(いくつかの数値ハックを使用):

    // where are 'fen' and 'moves' within the initial string?
    int fenPos = input.find("fen");
    int movesPos = input.find("moves");

    // get the exact string but taking into account the position of our initial tokens
    // as well as their length (e.g. fenPos.length()+1 = 4, etc)
    string fen = input.substr(fenPos+4,movesPos-fenPos-5);
    string moves = input.substr(movesPos+6);

入力の場合:

"position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 moves f2f3 e7e6 g2g4"、これは次を返します:

fen   = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
moves = "f2f3 e7e6 g2g4"
于 2013-01-21T03:40:36.670 に答える