0

getline 文字列を通常の文字列でトラバースする方法はありますか。

string nextLine;

getline(fin, nextLine);

nextLine = "2 BIRTH OCT 30 1998";

string stringTraverse = ?;

すべての単語が読み取られるまで、stringTraverse は「2」、次に「BIRTH」である必要があります。

4

2 に答える 2

2

nextLine.c_str() で sscanf を使用して、各ピースを取得できます。または、 nextLine を文字列ストリームに入れ、ストリームが完了するまで読み取ります。

stringstream s(nextLine);
while (s >> some_string)
    //do stuff with string piece
于 2011-07-15T02:22:08.970 に答える
0

以下は疑似ロジックです (テストされていませんが、そのように見えるはずです)。

size_t word = 0, currentSpace;
while(string::npos != (currentSpace = nextLine.find(" ")))
{
  stringTraverse = nextLine.substr(word, currentSpace);
  while(nextLine[++currentSpace] == " ");
  word = currentSpace;
  // ... use it
}
if(nextLine[word] != 0)
  stringTraverse = nextLine.substr(word);
于 2011-07-15T02:39:33.160 に答える