getline 文字列を通常の文字列でトラバースする方法はありますか。
string nextLine;
getline(fin, nextLine);
nextLine = "2 BIRTH OCT 30 1998";
string stringTraverse = ?;
すべての単語が読み取られるまで、stringTraverse は「2」、次に「BIRTH」である必要があります。
nextLine.c_str() で sscanf を使用して、各ピースを取得できます。または、 nextLine を文字列ストリームに入れ、ストリームが完了するまで読み取ります。
stringstream s(nextLine);
while (s >> some_string)
//do stuff with string piece
以下は疑似ロジックです (テストされていませんが、そのように見えるはずです)。
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);