std::getline は区切り文字を指定可能 ― 2020年01月30日 13時27分57秒
std::getline は名前の通り、一行を読み込むための API だが、第二引数として、区切り文字を指定できる。
このコードは e を区切り文字にしている。
#include <string>
#include <iostream>
int main()
{
std::string input;
while( std::getline( std::cin, input, 'e' ) )
{
std::cout << ':' << input << std::endl;
}
}
最後の e の後は、ctrl-D を打って入力が終了した後に処理されるので出力が若干読み取りづらい。
% ./a.out
abcdefg f e dcba
:abcd
:fg f
ctrl-D
: dcba
前回。
最近のコメント