basic_stream :: seekg()方法用于设置要从输入流中提取的下一个字符的位置。 iostream头文件中提供了此函数。以下是相同的语法:
头文件:
#include
句法:
basic_istream& seekg (pos_type pos);
范围:
- pos :代表缓冲区中的新位置。
返回值:该函数返回basic_istream对象。
下面是说明std :: basic_istream :: seekg()的程序
程序1:
// C++ code for basic_istream::seekg()
#include
using namespace std;
// Driver code
int main()
{
string str = "Geeks for Geeks";
istringstream gfg(str);
string a, b;
gfg >> a;
gfg.seekg(0); // rewind
gfg >> b;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
输出:
a = Geeks
b = Geeks
程式2:
// C++ code for basic_istream::seekg()
#include
using namespace std;
// Driver code
int main()
{
string str = "Geeks for Geeks";
istringstream gfg(str);
string a, b;
gfg >> a;
gfg.seekg(6); // rewind
gfg >> b;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
输出:
a = Geeks
b = for
参考: http : //www.cplusplus.com/reference/istream/basic_istream/seekg/
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。