Cpp-standard-library-cpp-basic-ios-unget
提供:Dev Guides
C ++ basic_iosライブラリ-unget
説明
キャラクターをアンゲットするために使用されます。
宣言
- 以下は、std
- basic_istream :: ungetの宣言です。
basic_istream& unget();
パラメーター
none
戻り値
basic_istreamオブジェクト(* this)を返します。
例外
基本保証-例外がスローされた場合、オブジェクトは有効な状態です。
データの競合
ストリームオブジェクトを変更します。
例
- 以下のstd
- basic_istream :: ungetの例。
#include <iostream>
#include <string>
int main () {
std::cout << "Please, enter a number or a word: ";
char c = std::cin.get();
if ( (c >= '0') && (c <= '9') ) {
int n;
std::cin.unget();
std::cin >> n;
std::cout << "You entered a number: " << n << '\n';
} else {
std::string str;
std::cin.unget();
getline (std::cin,str);
std::cout << "You entered a word: " << str << '\n';
}
return 0;
}
出力は次のようになります-
Please, enter a number or a word: 7791
You entered a number: 7791