El std::basic_istream::peek() solía leer el siguiente carácter del flujo de entrada sin extraerlo. Esta función no acepta ningún parámetro, simplemente devuelve el siguiente carácter en la string de entrada. A continuación se muestra la sintaxis para el mismo:
Archivo de cabecera:
#include<iostream>
Sintaxis:
int peek();
Valor devuelto: std ::basic_istream::peek() devuelve el siguiente carácter en la string de entrada.
A continuación se muestran los programas para comprender mejor la implementación de std::basic_istream::peek() :
Programa 1:
// C++ code for std::basic_istream::peek() #include <bits/stdc++.h> 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
Programa 2:
// C++ code for std::basic_istream::peek() #include <bits/stdc++.h> 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
Programa 3:
// C++ code for std::basic_istream::peek() #include <bits/stdc++.h> 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
Referencia: http://www.cplusplus.com/reference/istream/basic_istream/peek/
Publicación traducida automáticamente
Artículo escrito por bansal_rtk_ y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA