0

a をストリーミングしたいのstd::stringですが、最初の 2 文字または最後の 2 文字なしで実行できるようにしたいです。

例えば:

string foo{"bu blah blah blee le"};

cout << foo.substr(2) << endl << foo.substr(0, foo.size() - 2) << endl;

iomanipそのためのツールはありますか?それとも、先に進んで一時的なstrings を作成する必要がありますか?

4

1 に答える 1

1

使用できますcout.write

cout.write(foo.c_str() + 2, foo.size() - 4);

これもストリームを返すので、次のことができます。

cout << "First two: ";
cout.write(foo.c_str(), 2) << "\nAll but first two: ";
cout.write(foo.c_str() + 2, foo.size() - 2) << '\n';
于 2014-12-06T15:26:04.863 に答える