cin的詳細用法--day2

來源:互聯網
上載者:User

0、#include <iostream>

(1)istream cin; //定義一個輸入類對象

cin:標準輸入類對象佔用一定的記憶體空間

cin>>a;  (運算子多載)

功能:用於從標準的輸入裝置中擷取內容並且儲存到指定記憶體中

特點:可以用於擷取不同資料類型

    可以用於連續輸入,不過用"空格" "/" "Enter"來分隔  //cin>>a>>b;

    不可以在後面加上endl;

    cin可以調用方法

    cin如果想要擷取不確定字串內容,不要採用自訂資料形式

(2)get方法

class istream

{

//定義一個get方法

int_type get();

__istream_type&get(char_type&__c); //get函數傳參不一樣,個數不一樣,可以同名

__istream_type&get(char_type*__s,streamsize__n,char_type__delim);

__istream_type&get(char_type*__s,streamsize__n);

.......

}

函數重載

1、cin.get()

   它是一個讀取單個字元方法。字元變數=cin.get =>cin.get(字元變數)

#include<iostream>

using namespace std;

int main()

{

char str;

str = cin.get();//cin.get(str);

cout<<str<<endl;  //輸入ch之後輸出的字元為c

systerm("pause"); //進行停止,否則會一閃而過

}

include   using namespace std;       int main()   {     char str1;     char str2;     str1 = cin.get(); //讀取單個字元,在螢幕輸入     str2 = cin.get();     cout<<str1<<<<str2<<endl; //輸出剛剛載入的單個字元     system("pause");  //進行暫停,否則會一閃而過   }   輸入:abcd   輸出:ab

既然cin.get()是讀取第一個字元,那str2為什麼不也是a呢。
原理如下:
    在cin這個對象裡,有一個儲存字元的流,可以想象成緩衝區,事實上是cin裡封裝的一個東西.當我們在程式上輸入字元後,對象cin獲得了我們輸入的字元。例如獲得abcd,然後再通過.get()把流裡面的第一個字元去掉,賦給str1,這時,cin裡儲存的流的資料為bcd,而str1則獲得了a。當我們再次運行str2 = cin.get()時,同理把cin裡流的資料的b拿出來給了str2,此後,cin裡面的流的資料為cd,而str2則為b,所以最後輸出時,便能輸出ab了。

    還有個補充,究竟什麼時候才輸入資料呢。我們可以再通過上面的代碼進行嘗試,我們輸入單個字母'a',然後按斷行符號,發現並沒有輸出資料,而是再等待一次輸入資料,我們再輸入字母'b',按斷行符號後便輸出ab了。相信到這裡,大家都應該明白了,因為當我們第一次輸入a後,通過str1 = cin.get()使cin裡的流沒有資料,清空了。所以到第二次要再賦給str2值時,它找不到資料,要重新再輸入資料。由此來看可以知道,當cin裡的流資料清空時,便需要重新輸入才能賦值。而cin.get()還有個用法: [cpp]  view plain  copy  print ? #include    using namespace std;       int main()   {     char str1;     char str2;     str1 = cin.get(); //讀取單個字元,在螢幕輸入     cin.get();     str2 = cin.get();     cout<<str1<<str2<<endl; //輸出剛剛載入的單個字元     system("pause");   }  

運行程式後

輸入:abcd 輸出:ac

    程式中有3個cin.get(),由此可知,當空回調cin.get()時,cin.get便自動在cin中的流資料中刪除一個字母,起了一個刪除作用。

    對cin.get()有了一定瞭解之後,對cin.getline()的學習就可以更快了,原理是一致的,但是cin.getline()則是擷取一整行文本。以下是cin.getline()原形:
getline(char *line,int size,char='/n')
第一個是字元指標,第二個是字元長度,第三個為結束標識符。 2、cin.getline()

函數作用:從標準輸入裝置鍵盤讀取一串字串,並以指定的結束符結束。

1)__istream_type&getline(char_type* __s, streamsize __n)
2)__istream_type&getline(char_type*__s,streamsize__n,char_type__delim);

3)__istream_type&getline(char_type* __s, streamsize __n)
      { return this->getline(__s, __n, this->widen('\n')); }

.......

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    char cstr[20];
    cin.getline(cstr,sizeof(cstr),'x');
    cout <<cstr<<endl;
    system("pause");
    return 0;
}
 輸入:abcdx (enter鍵)   輸出:abcd 

       abx(enter鍵)                   ab

           xabc

3、cin.gount()

從緩衝區中讀取資料,輸出(資料)數組的位元組數,與cin.get/cin.getline搭配使用



streamsize gcount()  const {

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.