putback
描述 (Description)
它用于放回角色。
声明 (Declaration)
以下是std :: basic_istream :: putback的声明。
basic_istream& putback (char_type c);
参数 (Parameters)
c - 要放回的字符。
返回值 (Return Value)
返回basic_istream对象(* this)。
异常 (Exceptions)
Basic guarantee - 如果抛出异常,则对象处于有效状态。
数据竞争 (Data races)
修改流对象。
例子 (Example)
在下面的示例中为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