C++ 中用cin方式獲取輸入的幾種常用方式


一、前言  

  在C++程序的編寫過程中,可能會經常用到cin方式用來捕獲輸入設備的輸入信息。細分的話,主要的方式有:cin>>、cin.get、cin.getline。在借助鍵盤等字符輸入設備進行輸入的時候,如果鍵入Enter(\r)才會把目標字符輸入到緩存區,,鍵入的'\r'會被轉換成一個'\n',這個換行符同樣也會被輸入到緩存區,當做一個鍵入字符來處理。

  參考資料: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 <<  '\n' << 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,經過調試發現,並不會在最后出現截止符‘\0’。

  (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);【默認以換行符\n分隔行】

  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!

六、總結

  還有一些細節了解的並不是很清楚,需要進一步的學習和實踐。

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM