basic_istream::putback()用於將字符放回輸入字符串中。 iostream頭文件中提供了此函數。以下是相同的語法:
頭文件:
#include<iostream>
用法:
basic_istream& putback (char_type ch);
參數:
- ch:它表示要放回輸入字符串中的字符。
返回值:iostream::basic_istream::putback()返回basic_istream對象。
下麵的程序可以更好地了解std::basic_istream::putback()的實現:
程序1:
// C++ code for basic_istream::putback()
#include <bits/stdc++.h>
using namespace std;
int main()
{
stringstream gfg1("GeeksforGeeks");
gfg1.get();
// putback A into the input string
if (gfg1.putback('A'))
cout << gfg1.rdbuf() << endl;
istringstream gfg2("GeeksforGeeks");
gfg2.get();
if (gfg2.putback('A'))
cout << gfg2.rdbuf() << endl;
else
cout << "putback is failed here\n";
gfg2.clear();
// Again putback G in the string
if (gfg2.putback('G'))
cout << gfg2.rdbuf() << endl;
}
輸出:
AeeksforGeeks putback is failed here GeeksforGeeks
程序2:
// C++ code for basic_istream::putback()
#include <bits/stdc++.h>
using namespace std;
int main()
{
stringstream gfg1("GOOD");
gfg1.get();
// putback B into the input string
if (gfg1.putback('B'))
cout << gfg1.rdbuf() << endl;
istringstream gfg2("GOOD");
gfg2.get();
if (gfg2.putback('B'))
cout << gfg2.rdbuf() << endl;
else
cout << "putback is failed here\n";
gfg2.clear();
// Again putback G in the string
if (gfg2.putback('G'))
cout << gfg2.rdbuf() << endl;
}
輸出:
BOOD putback is failed here GOOD
參考:http://www.cplusplus.com/reference/istream/istream/putback/
相關用法
- C++ std::less用法及代碼示例
- C++ cin get()用法及代碼示例
- C++ iswprint()用法及代碼示例
- C++ std::is_copy_constructible用法及代碼示例
- C++ iswgraph()用法及代碼示例
- C++ std::bit_xor用法及代碼示例
- C++ std::remove_const用法及代碼示例
- C++ std::is_trivially_assignable用法及代碼示例
- C++ basic_istream::get()用法及代碼示例
- C++ std::has_virtual_destructor用法及代碼示例
- C++ cauchy_distribution a()用法及代碼示例
- C++ ratio_equal()用法及代碼示例
- C++ std::to_address用法及代碼示例
- C++ std::is_nothrow_destructible用法及代碼示例
- C++ std::remove_volatile用法及代碼示例
- C++ std::is_trivially_move_assignable用法及代碼示例
注:本文由純淨天空篩選整理自bansal_rtk_大神的英文原創作品 basic_istream::putback() in C++ with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。