鱼C论坛

 找回密码
 立即注册
查看: 3098|回复: 1

[技术交流] C++(4th):cin.ignore,cin.peek,cin.read,cin.gcount,cout.precision,cout.width

[复制链接]
发表于 2021-2-1 19:47:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 一叶枫残 于 2021-2-8 18:01 编辑

本笔记内容:cin.ignore,cin.peek,cin.read,cin.gcount,cout.precision,cout.width

上代码
ignore代码1号
  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4. {
  5.         char a[10];
  6.        
  7.         cout << "请输入字符串:";
  8.         cin.ignore(5);
  9.         cin.getline(a,10);
  10.        
  11.         cout << "a:" << a << endl;
  12.        
  13.         return 0;
  14. }
复制代码

本代码作用为,忽略输入的前5个字符(cin.ignore),把后面字符储存到a(cin.getline)当中,例
  1. 请输入字符串:0123456789
  2. a:56789
复制代码


ignore代码2号
  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4. {
  5.         char a[10];
  6.        
  7.         cin.ignore( 5 , 'a');
  8.         cin.getline(a,10);
  9.        
  10.         cout << a << endl;
  11.        
  12.         return 0;
  13. }
复制代码
  1. 请输入字符串:012a3456789
  2. a:3456789
复制代码
  1. 请输入字符串:0123456a789
  2. a:56a789
复制代码

这个代码的作用是,忽略前5个字符或者遇到字符a(ignore)时,把后面的字符赋10个字符值给a(cin.getline)

那么,如果想当遇到字符a再赋后面的值而不是选择忽略前面多少个字符该怎么办呢,经过检验,写成ignore( 0 , 'a')或者ignore( 'a' )不行,我们可以把前面参数的值设大点就ok,例如ignore( 100000 , 'a' );

cin.peek
上代码
  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4. {
  5.         int a;
  6.        
  7.         cout << "请输入a的值:" << endl;
  8.          
  9.         a = cin.peek();
  10.         cout << "a:" << a << endl;
  11.        
  12.         return 0;
  13.        
  14. }
复制代码
  1. 请输入a的值:
  2. 1425
  3. a:49
复制代码
  1. 请输入a的值:
  2. 3524
  3. a:51
复制代码


cin.peek只接受单个字符的输入,当输入多个字符,它只允许第一个字符的进入,注意是字符,当其给整型变量a赋值的时候,a被赋值的是字符对应的ascii码,如果a是字符类型,那么cout输出的a对应的就是输入的第一个字符;

cin.read
上代码
  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4. {
  5.         char a[50];
  6.        
  7.         cout << "请输入a的值:" << endl;
  8.          
  9.         cin.read( a , 20 );
  10.         cout << "a:" << a << endl;
  11.        
  12.         return 0;
  13.        
  14. }
复制代码
  1. 请输入a的值:
  2. 0123456789
  3. 0123456789
  4. a:0123456789
  5. 012345678
复制代码

cin.read的作用是读取指定长度的字符串,它与cin.getline的作用相似,但是cin.read不局限于行,当没读取足够长度的字符串时,还需要输入

cin.count
  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4. {
  5.         char a[20];
  6.        
  7.         cout << "请输入a的值:" << endl;
  8.          
  9.         cin.read( a , 20 );
  10.        
  11.         cout << "a:" << a << endl;
  12.         cout << "收集的字符串长度为:" << cin.gcount() << endl;
  13.        
  14.         return 0;
  15.        
  16. }
复制代码
  1. 请输入a的值:
  2. 01234567890123456789
  3. a:01234567890123456789
  4. 收集的字符串长度为:20
复制代码


cin.gcount可计算取得的字符串长度,但是,看如下两个代码
  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4. {
  5.         char a[50];
  6.        
  7.         cout << "请输入a的值:" << endl;
  8.          
  9.         cin.read( a , 20 );
  10.        
  11.         cout << "a:" << a << endl;
  12.         cout << "收集的字符串长度为:" << cin.gcount() << endl;
  13.        
  14.         return 0;
  15.        
  16. }
复制代码
  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4. {
  5.         char a[50];
  6.        
  7.         cout << "请输入a的值:" << endl;
  8.          
  9.         cin.getline( a , 20 );
  10.        
  11.         cout << "a:" << a << endl;
  12.         cout << "收集的字符串长度为:" << cin.gcount() << endl;
  13.        
  14.         return 0;
  15.        
  16. }
复制代码


对于第一个程序:
  1. 请输入a的值:
  2. 01234567890123456789
  3. a:01234567890123456789        曠@
  4. 收集的字符串长度为:20
复制代码


第二个程序:
  1. 请输入a的值:
  2. 01234567890123456789
  3. a:0123456789012345678
  4. 收集的字符串长度为:19
复制代码


两个结果各有不同,具体原因暂不知,注意使用;

cout.precision
  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4. {
  5.         float a;
  6.         a = 3.141596254;
  7.         cout << "a:" << a << endl << endl;
  8.        
  9.         int i;
  10.         for( i = 0; i <= 9; i++)
  11.         {
  12.                 cout.precision(i);
  13.                 cout << a << endl;
  14.         }
  15.        
  16.         return 0;
  17.        
  18. }
复制代码
  1. a:3.1416

  2. 3
  3. 3
  4. 3.1
  5. 3.14
  6. 3.142
  7. 3.1416
  8. 3.1416
  9. 3.141596
  10. 3.1415963
  11. 3.14159632
复制代码

cout.precision的作用是用于控制输出浮点数的精度;


cout.width
  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4. {
  5.         float a;
  6.         a = 3.14;
  7.         cout << "a:" << a << endl << endl;
  8.        
  9.         int i;
  10.         for( i=0; i<=8; i++)
  11.         {
  12.                 cout.width(i);
  13.                 cout << a << endl;               
  14.         }
  15.        
  16.         return 0;
  17. }
复制代码
  1. a:3.14

  2. 3.14
  3. 3.14
  4. 3.14
  5. 3.14
  6. 3.14
  7. 3.14
  8.   3.14
  9.    3.14
  10.     3.14
复制代码

cout.width的作用是用于控制输出的宽度,但前提是把内容给输出完才开始控制宽度

评分

参与人数 2荣誉 +1 鱼币 +2 收起 理由
三刀流.索隆 + 1 感谢楼主无私奉献
苏安酸 + 1 + 1 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-24 16:21:48 | 显示全部楼层
最后一个例子的输出结果的第八行3.14前少了一个空格
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 16:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表