operator<<,>> (std::basic_string)
|
ヘッダーで定義
<string>
|
||
|
template
<
class
CharT,
class
Traits,
class
Allocator
>
std::
basic_ostream
<
CharT, Traits
>
&
|
(1) | |
|
template
<
class
CharT,
class
Traits,
class
Allocator
>
std::
basic_istream
<
CharT, Traits
>
&
|
(2) | |
その後、結果のシーケンス seq ( str の内容にパディングを加えたもの)の各文字を、 os. rdbuf ( ) - > sputn ( seq, n ) を呼び出すかのように出力ストリーム os に挿入する。ここで n は std:: max ( os. width ( ) , str. size ( ) ) である。最後に、 os. width ( 0 ) を呼び出して、 std::setw の効果があればそれをキャンセルする。
|
以下と等価: return os << std:: basic_string_view < CharT, Traits > ( str ) ; |
(C++17以降) |
-
N文字が読み取られた場合(Nは is. width ( ) が is. width ( ) > 0 の場合はその値、それ以外の場合は str. max_size ( ) )、 - ストリーム is でEOF状態が発生した場合、または
- std:: isspace ( c, is. getloc ( ) ) が true となる文字 c が is に次に存在する場合(この空白文字は入力ストリームに残る)。
文字が一つも抽出されなかった場合、 std::ios::failbit が is に設定され、 std::ios_base::failure がスローされる可能性がある。
最後に、 is. width ( 0 ) を呼び出して std::setw の効果があればそれを解除する。
目次 |
例外
パラメータ
| os | - | 文字出力ストリーム |
| is | - | 文字入力ストリーム |
| str | - | 挿入または抽出される文字列 |
戻り値
例
#include <iostream> #include <sstream> #include <string> int main() { std::string greeting = "Hello, whirled!"; std::istringstream iss(greeting); std::string hello_comma, whirled, word; iss >> hello_comma; iss >> whirled; std::cout << greeting << '\n' << hello_comma << '\n' << whirled << '\n'; // Reset the stream iss.clear(); iss.seekg(0); while (iss >> word) std::cout << '+' << word << '\n'; }
出力:
Hello, whirled! Hello, whirled! +Hello, +whirled!
不具合報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用バージョン | 公開時の動作 | 修正後の動作 |
|---|---|---|---|
| LWG 25 | C++98 | n は os. width ( ) と str. size ( ) の小さい方 | n は大きい方 |
| LWG 90 | C++98 |
std::
isspace
(
c, getloc
(
)
)
でスペースをチェックしていたが、
getloc
は
<string>
で宣言されていない
|
getloc
(
)
を
is. getloc ( ) に置換 |
| LWG 91 | C++98 |
operator>>
が
FormattedInputFunction として動作しなかった |
FormattedInputFunction として動作 |
| LWG 211 | C++98 |
operator>>
が文字が抽出されない場合に
failbit
を設定しなかった
|
failbit
を設定
|
| LWG 435 | C++98 |
文字は
os.
rdbuf
(
)
-
>
sputn
(
str.
data
(
)
, n
)
で挿入され、
LWG issue 25 の解決により os. width ( ) が str. size ( ) より大きい場合の動作が未定義 |
パディングを先に決定し、
パディングされた文字列を 挿入するように変更 |
| LWG 586 | C++98 |
operator<<
が
FormattedOutputFunction として動作しなかった |
FormattedOutputFunction として動作 |
関連項目
|
(C++17)
|
文字列ビューに対するストリーム出力を実行する
(関数テンプレート) |