std::basic_streambuf<CharT,Traits>::sputbackc
來自 cppreference.com
< cpp | io | basic_streambuf
int_type sputbackc( char_type c ); |
||
將字元放回獲取區。
如果獲取區中存在回放位置 (gptr() > eback()),並且字元 c
等於 gptr() 左側一個位置的字元(由 Traits::eq(c, gptr()[-1]) 確定),則簡單地遞減下一個指標 (gptr())。
否則,呼叫 pbackfail(Traits::to_int_type(c)) 以回溯獲取區或修改獲取區和可能關聯的字元序列。
I/O 流函式 basic_istream::putback 是根據此函式實現的。
目錄 |
[編輯] 引數
c | - | 要放回的字元 |
[編輯] 返回值
如果回放位置可用,則返回下一個指標當前指向的字元,並使用 Traits::to_int_type(*gptr()) 轉換為 int_type
。此 streambuf 的下一個單字元輸入將返回此字元。
如果回放位置不可用,則返回 pbackfail() 的返回值,失敗時為 Traits::eof()。
[編輯] 示例
執行此程式碼
#include <iostream> #include <sstream> int main() { std::stringstream s("abcdef"); // gptr() points to 'a' in "abcdef" std::cout << "Before putback, string holds " << s.str() << '\n'; char c1 = s.get(); // c1 = 'a', gptr() now points to 'b' in "abcdef" char c2 = s.rdbuf()->sputbackc('z'); // same as s.putback('z') // gptr() now points to 'z' in "zbcdef" std::cout << "After putback, string holds " << s.str() << '\n'; char c3 = s.get(); // c3 = 'z', gptr() now points to 'b' in "zbcdef" char c4 = s.get(); // c4 = 'b', gptr() now points to 'c' in "zbcdef" std::cout << c1 << c2 << c3 << c4 << '\n'; s.rdbuf()->sputbackc('b'); // gptr() now points to 'b' in "zbcdef" s.rdbuf()->sputbackc('z'); // gptr() now points to 'z' in "zbcdef" int eof = s.rdbuf()->sputbackc('x'); // nothing to unget: pbackfail() fails if (eof == EOF) std::cout << "No room to putback after 'z'\n"; }
輸出
Before putback, string holds abcdef After putback, string holds zbcdef azzb No room to putback after 'z'
[編輯] 參閱
將輸入序列中的下一個指標後退一個位置 (公有成員函式) | |
將字元放入輸入流 ( std::basic_istream<CharT,Traits> 的公有成員函式) |