📜  C++中的basic_istream :: readsome()及其示例

📅  最后修改于: 2021-05-30 07:07:14             🧑  作者: Mango

basic_istream :: readsome()用于从缓冲区读取数据,并从输入字符串提取最多n个立即可用的字符。此函数返回提取的字符。以下是相同的语法:

头文件:

#include

句法:

streamsize readsome(char_type* a,
                    streamsize n);

参数:

  • n:代表要读取的最大字符数。
  • a:它是存储提取的字符的数组的指针。

返回值: std :: basic_istream :: readsome()返回提取的字符。

下面的程序可以更好地了解std :: basic_istream :: readsome()的实现:

程序1:

// C++ code for basic_istream::readsome()
#include 
using namespace std;
  
// main method
int main()
{
    char gfg[50] = {};
    istringstream gfg1("GeeksforGeeks");
  
    // reads 'Gee' and
    // stores in c[0] .. c[2]
    gfg1.readsome(gfg, 3);
  
    // reads 'ksforG' and
    // stores in c[0] .. c[5]
    gfg1.readsome(gfg, 6);
    cout << gfg << endl;
}
输出:
ksforG

程式2:

// C++ code for basic_istream::readsome()
#include 
using namespace std;
  
// main method
int main()
{
    char gfg[50] = {};
    istringstream gfg1("Computer");
  
    // reads 'Co' and
    // stores in c[0] .. c[1]
    gfg1.readsome(gfg, 2);
  
    // reads 'mpu' and
    // stores in c[0] .. c[4]
    gfg1.readsome(gfg, 3);
    cout << gfg << endl;
}
输出:
mpu

参考: http : //www.cplusplus.com/reference/istream/basic_istream/readsome/

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”