std :: basic_istream :: peek()用于从输入流中读取下一个字符,而无需提取它。该函数不接受任何参数,仅返回输入字符串的下一个字符。以下是相同的语法:
头文件:
#include
句法:
int peek();
返回值: std :: basic_istream :: peek()返回输入字符串的下一个字符。
下面的程序可以更好地了解std :: basic_istream :: peek()的实现:
程序1:
// C++ code for std::basic_istream::peek()
#include
using namespace std;
// main method
int main()
{
istringstream gfg("GeeksforGeeks");
char c1 = gfg.peek();
char c2 = gfg.get();
char c3 = gfg.get();
cout << "The first character is: "
<< c1 << endl
<< " and the next is: "
<< c3 << endl;
}
输出:
The first character is: G
and the next is: e
程式2:
// C++ code for std::basic_istream::peek()
#include
using namespace std;
// main method
int main()
{
istringstream gfg("Computer");
char c1 = gfg.peek();
char c2 = gfg.get();
char c3 = gfg.get();
cout << "The first character is: "
<< c1 << endl
<< " and the next is: "
<< c3 << endl;
}
输出:
The first character is: C
and the next is: o
程序3:
// C++ code for std::basic_istream::peek()
#include
using namespace std;
// main method
int main()
{
istringstream gfg("Laptop");
char c1 = gfg.peek();
char c2 = gfg.get();
char c3 = gfg.get();
char c4 = gfg.get();
cout << "The first character is: "
<< c1 << endl
<< " and the next is: "
<< c3 << endl
<< " after that next is: "
<< c4 << endl;
}
输出:
The first character is: L
and the next is: a
after that next is: p
参考: http : //www.cplusplus.com/reference/istream/basic_istream/peek/
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。