Cpp-standard-library-cpp-istream-sentry
提供:Dev Guides
C ++ Istreamライブラリ-歩sent
説明
入力用のストリームを準備するために使用されます。 入力操作を実行するすべてのメンバー関数は、このクラスのオブジェクトを自動的に構築して評価します(状態フラグが設定されていない場合はtrueを返します)。 このオブジェクトがtrueと評価された場合のみ、関数は入力操作を試行します(そうでない場合、実行せずに戻ります)。 戻る前に、関数はセントリーオブジェクトを破棄します。
宣言
- 以下は、std
- basic_istream :: sentryの宣言です。
C 98
class sentry {
public:
explicit sentry (basic_istream& is, bool noskipws = false);
~sentry();
operator bool() const;
private:
sentry (const sentry&);
sentry& operator= (const sentry&);
};
C 11
class sentry {
public:
explicit sentry (basic_istream& is, bool noskipws = false);
~sentry();
explicit operator bool() const;
sentry (const sentry&) = delete;
sentry& operator= (const sentry&) = delete;
};
メンバー
- *明示的な歩ry(basic_istream&is、bool noskipws = false); *-出力操作のために出力ストリームを準備し、上記のアクションを実行します。
- *〜sentry(); *-操作を実行しません(実装定義)。
- *明示的な演算子bool()const; *-オブジェクトが評価されると、sentryコンストラクタがすべてのタスクを正常に実行したかどうかを示すbool値を返します:構築プロセスのある時点で内部エラーフラグが設定された場合、この関数そのオブジェクトに対して常にfalseを返します。
例
- 以下の例では、std
- basic_istream :: sentryについて説明しています。
#include <iostream>
#include <string>
#include <sstream>
#include <locale>
struct Phone {
std::string digits;
};
std::istream& operator>>(std::istream& is, Phone& tel) {
std::istream::sentry s(is);
if (s) while (is.good()) {
char c = is.get();
if (std::isspace(c,is.getloc())) break;
if (std::isdigit(c,is.getloc())) tel.digits+=c;
}
return is;
}
int main () {
std::stringstream parseme (" (555)2326");
Phone myphone;
parseme >> myphone;
std::cout << "digits parsed: " << myphone.digits << '\n';
return 0;
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
digits parsed: 5552326