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