zoukankan      html  css  js  c++  java
  • C++ 中用cin方式获取输入的几种常用方式

    一、前言  

      在C++程序的编写过程中,可能会经常用到cin方式用来捕获输入设备的输入信息。细分的话,主要的方式有:cin>>、cin.get、cin.getline。在借助键盘等字符输入设备进行输入的时候,如果键入Enter( )才会把目标字符输入到缓存区,,键入的' '会被转换成一个' ',这个换行符同样也会被输入到缓存区,当做一个键入字符来处理。

      参考资料:https://blog.csdn.net/k346k346/article/details/48213811

    二、测试环境

      Win10 + Visual Studio 2017 + Debug (x86)

    三、cin>>方式

      可以从输入设备连续提取输入信息,其中,可以作为分格符使用的有Tab、空格(Space),换行符是用作结束符使用。

     1 #include "pch.h"
     2 #include <iostream>
     3 #include <string>
     4 #include <vector>
     5 
     6 using namespace std;
     7 
     8 int main(void) {
     9     string str;
    10     while (cin >> str) {
    11         cout << str <<  '
    ' << endl;
    12     }
    13     return 0;
    14 }

      测试结果如下【输入:orange[Space]juice[Tab]orange[Enter]】。

    1 orange juice    orange
    2 orange
    3 
    4 juice
    5 
    6 orange

      这种方式下的str,经过调试发现,并不会在最后出现截止符‘’。

      (1)、cin>> 等价于cin.operator>>(),调用成员函数operator>>()进行数据的读取;

      (2)、当cin>>方式从缓存区读取数据时,会自动忽略作为第一个字符存在的Tab、空格(Space)、换行,继续读取下一个字符,若缓存区为空,则继续等待。但是如果读取成功,字符后面的分隔符是残留在缓冲区的,cin>>不做处理。

      个人觉得体验不好的一点:结束输入的时候,需要【Ctrl + Z】+ 【Enter】,连续两次的输入才能达到目的。【经过查询,有的网友认为是编译器的原因,这里还没有进行进一步的确定】

    四、cin.get方式

      cin..get方式用来读取一个字符,包括空格【Space】。

      主要的实现方式有两种:cin.get、cin.get(para)。

     1 #include "pch.h"
     2 #include <iostream>
     3 #include <string>
     4 #include <vector>
     5 
     6 using namespace std;
     7 
     8 int main(void) {
     9     char a, b;
    10     a = cin.get();
    11     cin.get(b);
    12     cout << a << b << endl;
    13     system("pause");
    14     return 0;
    15 }

     测试结果如下【输入:cv[Enter]】。

    cv
    cv
    请按任意键继续. . .

      需要注意的是,cin.get()从输入缓存区读取单个字符时,并不会忽略分割符;cin.get()的返回值是int类型,成功读取的时候则返回字符的ASCII码值,遇到文件结束符时则返回EOF(-1)【Windows下的结束符:Ctrl + z;Linux下的结束符:Ctrl + d】;cin.get(para)在读取成功的时候返回的是cin对象,对此,我们可以进行链式操作,比如cin.get(a).get(b)。

      cin.get()方式用来读取一行的流数据,有两种方式:

      istream& get (char* s, streamsize n)【默认以换行符结束】;

      istream& get (char* s, size_t n, streamsize delin)【可指定结束符,n表示目标空间的大小】。

     1 #include "pch.h"
     2 #include <iostream>
     3 #include <string>
     4 #include <vector>
     5 
     6 using namespace std;
     7 
     8 int main(void) {
     9     char a;
    10     char array[10] = { NULL };
    11 
    12     cin.get(array, 10);
    13     cin.get(a);
    14     cout << array << ' '<< (int)a << endl;
    15     
    16     system("pause");
    17     return 0;
    18 }

       结果1【输入:0123456789[Space][Enter]】。

    1 0123456789
    2 012345678 57

      结果2【输入:012345678[Space][Enter]】

    1 012345678
    2 012345678 32

      结果3【输入:01234567[Space][Enter]】

    1 01234567
    2 01234567  10

      cin.get(array, 10)在进行数据读取时,遇到换行符结束,但是不对换行符进行处理。如果换行符之前数据个数超过9【包括[Space]】,则array读取的是前9个。对于换行符,cin.get(a)会对其进行相应的处理。

    五、cin.getline方式

      cin.getline():从标准输入设备读取一串字符串,并以指定的结束符结束。

      相关的函数原型有:

      istream& getline(char* s, streamsize count)【默认以换行符结束】; 

      istream& getline(char* s, streamsize count, char delim)【delim用以指定结束符】。

     1 #include "pch.h"
     2 #include <iostream>
     3 #include <string>
     4 #include <vector>
     5 
     6 using namespace std;
     7 
     8 int main(void) {
     9     char a;
    10     char array[10] = { NULL };
    11 
    12     cin.getline(array, 10);
    13     cout << array << endl;
    14     
    15     system("pause");
    16     return 0;
    17 }

      测试结果1【输入:12345678978[Space][Enter]】。

    1 12345678978
    2 123456789

      测试结果2【输入:12345678[Space][Enter]】。

    1 12345678
    2 12345678

      cin.getline()方式不会将结束符或者是换行符输入到缓存区中。

    五、其他的一些方式

      1、getline

      getline:定义在namespace的全局函数,声明放在了<string>文件中。、

      getline利用cin可以从标准输入设备键盘读取一行,当遇到如下三种情况会结束读操作:

      (1)、文件结束;

      (2)、行分割符的出现;

      (3)、输入达到最大限度。

      函数原型有两种:

      istream& getline ( istream& is, string& str);【默认以换行符 分隔行】

      istream& getline ( istream& is, string& str, char delim)。

     1 #include "pch.h"
     2 #include <iostream>
     3 #include <string>
     4 #include <vector>
     5 
     6 using namespace std;
     7 
     8 int main(void) {
     9     string str;
    10 
    11     getline(cin, str);
    12     
    13     system("pause");
    14     return 0;
    15 }

      测试结果【输入:Hello World![Enter]】

      cin.getline()的输出是char*,getline()的输出是string。cin.getline()属于istream流,getline()属于string流,有一定的区别。

      2、gets

      gets属于C语言的库函数,在<stdio.h>中申明。

      从标准输入设备读取字符串,可以无限读取,不会判断上限,以回车结束或者EOF时停止读取。在实际的应用过程中,应该确保缓存空间足够大,以防发生溢出。

      函数原型:char *gets( char *buffer )。

     1 #include "pch.h"
     2 #include <iostream>
     3 #include <string>
     4 #include <vector>
     5 
     6 using namespace std;
     7 int main(void) {
     8     char array[20] = { NULL };
     9 
    10     gets_s(array);
    11 
    12     cout << array << endl;
    13 
    14     system("pause");
    15     return 0;
    16 }

       测试结果【输入:Hello World![Enter]】

    1 Hello World!
    2 Hello World!

    六、总结

      还有一些细节了解的并不是很清楚,需要进一步的学习和实践。

      

  • 相关阅读:
    vue 对图片进行拖拽到另一个位置
    vue自定义拖动指令
    使用pm2启动nodejs+express+mysql管理系统步骤
    重新学习html和css
    vue监听页面大小变化重新刷新布局
    Docker可视化管理工具DockerUI ,Portainer ,Shipyard对比(转)
    js删除html标记 去掉所有html标记 百度文库内容copy
    安卓模拟器连接端口一览表
    springmvc在使用@ModelAttribute注解获取Request和Response会产生线程并发不安全问题(转)
    常用软件测试工具的对比
  • 原文地址:https://www.cnblogs.com/wyt123/p/10698102.html
Copyright © 2011-2022 走看看