public member function
<istream> <iostream>

std::istream::unget

istream& unget();
Put back character
嘗試將流的當前位置減少一個字元,使從流中提取的最後一個字元再次可供提取操作使用。

Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to true). Then (if good), it calls sungetc on its associated stream buffer object (if any). Finally, it destroys the sentry object before returning.

如果在呼叫之前設定了 eofbit 標誌,則函式失敗(設定 failbit 並返回)。
該函式會清除在呼叫之前設定的 eofbit 標誌。

If the call to sungetc fails, the function sets the badbit flag. Note that this may happen even if the function is called right after a character has been extracted from the stream (depending on the internals of the associated stream buffer object).

呼叫此函式會將gcount的返回值設定為零。

引數



返回值

istream 物件 (*this)。

錯誤透過修改 *內部狀態標誌* 來發出訊號。
flagerror
eofbit-
failbit構造sentry失敗(例如,當流狀態在呼叫之前不是good時)。
badbitEither the internal call to sungetc failed, or another error occurred on the stream (such as when the function catches an exception thrown by an internal operation, or when no stream buffer is associated with the stream).
When set, the integrity of the stream may have been affected.
Multiple flags may be set by a single operation.

If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure.

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// istream::unget example
#include <iostream>     // std::cin, std::cout
#include <string>       // std::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


資料競爭

修改流物件。
併發訪問同一個流物件可能導致資料爭用。

異常安全

基本保證:如果丟擲異常,物件處於有效狀態。
It throws an exception of member type failure if the resulting error state flag is not goodbit and member exceptions was set to throw for that state.
Any exception thrown by an internal operation is caught and handled by the function, setting badbit. If badbit was set on the last call to exceptions, the function rethrows the caught exception.

另見