C++标准输入流相关函数

C++标准输入流相关函数

1.1 getline() c-string

  • 定位:std::istream::getline,公有成员函数

  • 函数原型:

    #include<iostream>
    #include<string>
    
    istream& getline (char* s, streamsize n );
    istream& getline (char* s, streamsize n, char delim );
    

    参数1:s,提取流来指定的目标缓冲。

    参数2:n,可写入缓冲的最大字节。

    [参数3:delim,划界字符。]

    说明:从流中读取一行可包括空白符的字符,当遇到划界字符(默认是’\n’),则读取结束,划界符字符将会被抛弃(缓冲中也不保留),并在字符串尾部自动加上结束符‘\0’。

    If the delimiter is found, it is extracted and discarded (i.e. it is not stored and the next input operation will begin after it).

  • 示例:

    	char s[10] = { 0 };
    	while (cin.getline(s, 6))	//不读取划界符,缓冲中也不保留划界符
    	{
    		cout << s;
    		char c;
    		cin.get(c);	/*读不到'\n',说明换行符并没有作为getline的数据也没有保留在缓冲中,而是被抛弃了。*/
    		cout << c;
    	}
    
  • 总结,用geline以c-string风格保存字符串,直到遇到划界符结束读取。划界符不作为内容读取也不保留在缓冲中。

1.2 getline() string

  • 定位:std::getline (string),普通函数

  • 函数原型:

    #include<iostream>
    #include<string>
    
    istream& getline (istream&  is, string& str, char delim);
    istream& getline (istream&& is, string& str, char delim);
    istream& getline (istream&  is, string& str);
    istream& getline (istream&& is, string& str);
    
    

    参数1:is,从标准输入流is中提取。

    参数2:str,保存在目的string对象str中。

    [参数3:划界符]

    说明:

    1. 从标准输入中提取一行字符包括空白符,并保存到string对象str中,直到遇到划界符(默认’\n’)完成读取。
    2. 提取过程中,当遇到eof标志或者遇到错误,将提取停止。
    3. 如果遇到划界符,它将不被作为提取的内容留在标准输入流中,下一次提取操作将从该划界符的下一个位置开始读取。
    4. 调用getline()函数提取数据将会覆盖上一次保存在str中的数据。
    5. 每次提取的字符会追加在字符串的尾部。并在字符串尾部自动加上结束符‘\0’。
  • 示例:

    	string str;
    	getline(cin, str);	//不读取划界符,缓冲中也不保留划界符
    	cout << str;
    	char c;
    	cin.get(c);
    	cout << c;
    
  • 总结,geline以string类型保存字符串,直到遇到划界符结束读取。划界符不作为内容也不保留在缓冲中。与c-string风格的geline区别在于字符串的类型。

1.3 get()

  • 定位:std::istream::get,成员方法

  • 函数原型:

    #include<iostream>
    //single character
    int get();
    istream& get(char& c);
    //c-string
    istream& get(char* s, streamsize n);
    istream& get(char* s, streamsize n, char delim);
    //stream buffer
    istream& get(streambuf& sb);
    istream& get(streambuf& sb, char delim);
    

    参数:c,保存一个字符。

    参数:s,保存以C-string风格的字符串。(如果缓冲中只有划界符并且参数n > 0,则s将设置为空字符串)。

    [参数:delim,划界符,(一旦要提取的字符为划界符,就停止从流中提取)。]

  • 说明

    重点:c-string:

    从缓冲中读取n-1个字符并以c-string风格进行储存。当遇到划界符(一般为’\n’)停止读取。划界符不作为读取的内容保存到s中,但保留在缓冲中作为下一个要读取的字符。每次读取完,结束符’\0’会追加到s的尾部作为结束标志。

  • 示例:int get()

    	int n;
    	while ((n = cin.get()) != '\n')// 回车符也从缓冲中取走了
    	{
    		cout << n;
    	}
    	n = cin.get();	//缓冲中没数据
    	cout << n;
    
  • 示例:istream& get(char& c)

    	char c;
    	while (cin.get(c))	//读取字符(包括换行)
    	{
    			cout << c;
    	}
    

上述两个示例都是字符操作,对划界符进行读取。

  • 示例 istream& get(char*s, streamsize n)

    	char s[10];
    	cin.get(s, 6);	// 不读取划界符
    	cout << s;
    	char c;
    	c = cin.get();	//从缓冲区中读取到'\n'
    	cout << c;
    

上述示例是对c-string字符串的操作,不对划界符进行读取,但保留在缓冲中作为下一个要读取的字符。

1.4 小结

字符串读取:

getgetline
划界符字符串不读取划界符字符串不读取划界符
缓冲保留划界符不保留划界符
最大字符数需要指定读取字符的最大数量不用制定最大可读数量

c-string风格和string风格

getline() c-stringgetline() string
字符串类型char*string
调用方式cin.getline(s, 6)getline(cin, str);
最大字符数需要指定读取字符的最大数量不用指定最大可读数量

2.1 ignore

  • 定位:std::istream::ignore,成员方法

  • 函数原型:

    #include<iostream>
    istream& ignore (streamsize n = 1, int delim = EOF);
    

    参数1:n,提取字符数量的的最大值(然后抛弃)

    参数2:delim,划界符

  • 说明:从输入缓冲队列中读取字符然后抛弃,直到读取了n个字符或读取到划界符则停止读取。注意,划界符也将被作为读取的内容,然后被抛弃。

  • 示例:

    char s[10] = {0};
    cin.get(s, 6);	//可读5个字符 + '\0'
    cin.ignore(256, ' ');	/*从缓冲中读取字符,读完256个字符或遇到指定的划界符停止;	*/
    char s2[10] = { 0 };
    string str;
    getline(cin, str);
    cout << s << str << endl;
    

    运行:输入hello world

    结果:

    helloworld
    

2.2 peek

  • 定位:std::istream::peek,成员函数

  • 函数原型:

    int peek();
    
  • 说明:

    返回输入缓冲中下个要读取的字符,注意,该字符并没有从缓冲中提取出来。相当于只是拷贝了缓冲中的首个字符作为返回值。

    Returns the next character in the input sequence, without extracting it: The character is left as the next character to be extracted from the stream.

  • 示例:

    	char buf1[256] = { 0 };
    	char buf2[256] = { 0 };
    	cin >> buf1;	//传入hello world,读取hello,空格+world+\n 留在缓冲中
    	cout << "buf1: " << buf1 << endl;	//输出hello
    	cin.ignore(2);	//抛弃 空格+w
    	char ch = cin.peek();	//拷贝缓冲队列的一个字符
    	cout << ch << endl;	//输出 o
    	cin.getline(buf2, 256);	// 读入 orld,抛弃\n
    	cout << "buf2: " << buf2;	//输出orld
    

2.3 putback

  • 定位:std::istream::putback, 成员函数

  • 函数原型:

    istream& putback (char c);
    

    参数:返回给缓冲的字符C

  • 说明:

    缓冲当前位置往前移一个字符的位置,将c返回给缓冲中当前的位置。

  • 示例:

    	cout << "请输入一个字符串或者数字:" << endl;
    	char ch = cin.get();//从缓冲区拿到第一个字符
    	if (ch >= '0' && ch <= '9') {
    		int number;
    		cin.putback(ch);	//把第一个字符返回给缓冲
    		cin >> number;
    		cout << "数字:" << number << endl;
    	}
    	else {
    		char buf[1024] = { 0 };
    		cin.putback(ch);
    		cin.getline(buf, 1024);
    		cout << "字符串:" << buf << endl;
    	}
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值