効果を得る最もクリーンな方法は何istream::getline(string, 256, '\n' OR ';')
ですか?
ループを書くのはとても簡単なことですが、何かが足りないのではないかと感じています。私ですか?
私が使用したもの:
while ((is.peek() != '\n') && (is.peek() != ';'))
stringstream.put(is.get());
効果を得る最もクリーンな方法は何istream::getline(string, 256, '\n' OR ';')
ですか?
ループを書くのはとても簡単なことですが、何かが足りないのではないかと感じています。私ですか?
私が使用したもの:
while ((is.peek() != '\n') && (is.peek() != ';'))
stringstream.put(is.get());
残念ながら、複数の「行末」を設定する方法はありません。あなたができることは、例えばで行を読み、std::getline
それをに入れて、のループで(セパレータstd::istringstream
を使って)使用することです。std::getline
';'
istringstream
Boost iostreamsライブラリをチェックして、機能があることを確認できますが。
std::getlineがあります。より複雑なシナリオでは、 istream_iteratorまたはistreambuf_iteratorをブースト分割またはregex_iteratorで分割してみてください(ストリーム イテレータの使用例を次に示します)。
これが実用的な実装です:
enum class cascade { yes, no };
std::istream& getline(std::istream& stream, std::string& line, const std::string& delim, cascade c = cascade::yes){
line.clear();
std::string::value_type ch;
bool stream_altered = false;
while(stream.get(ch) && (stream_altered = true)){
if(delim.find(ch) == std::string::npos)
line += ch;
else if(c == cascade::yes && line.empty())
continue;
else break;
}
if(stream.eof() && stream_altered) stream.clear(std::ios_base::eofbit);
return stream;
}
このcascade::yes
オプションは、見つかった連続する区切り文字を折りたたみます。を使用cascade::no
すると、検出された 2 番目の連続する区切り文字ごとに空の文字列が返されます。
使用法:
const std::string punctuation = ",.';:?";
std::string words;
while(getline(istream_object, words, punctuation))
std::cout << word << std::endl;
使い方を見るLive on Coliru
より一般的なバージョンはこれになります