C和C++鍵盤輸入函數總結__Jquery

來源:互聯網
上載者:User
1.C中字串輸入函數gets()

說明:可以讀入包括空格、跳格和斷行符號在內的空白字元 .

1.gets()用於一維字串中,可以接受空格  #include<stdio.h>//gets()包含在標頭檔stdio.h中  char *s;  gets(s);
2.gets(s)用於二維數組中,可以接受空格#include<iostream>#include<string>using namespace std;main (){    char m[3][20];    for(int i=0;i<3;i++)    {        gets(m[i]);    }    cout<<endl;    for(int j=0;j<3;j++)        cout<<m[j]<<endl;}
2.C++中——cin.get()

說明:可以讀入包括空格、跳格和斷行符號在內的空白字元 .

 用法一:cin.get(字元變數名);//可以用來接收單個字元
用法二:cin.get(字元數組名,接受個數);//接受字串,空格#include<iostream>#include<string>using namespace std;main (){    char m[3][20];    for(int i=0;i<3;i++)    {        cin.get(m[i],20);    }    cout<<endl;    for(int j=0;j<3;j++)        cout<<m[j]<<endl;}
3.C++中cin.getline(參數1,參數2,參數3);

說明:說明:可以讀入包括空格、跳格和斷行符號在內的空白字元 .

用法一:cin.getline(m,5);//m為數組名即指標,5個接受字串個數。#include <iostream>using namespace std;main (){char m[20];cin.getline(m,5);cout<<m<<endl;}
用法二:cin.getline(m,5,結束字元如'\0'或其他'a');參數3是字串輸入的結束字元。
4.getline(cin,str)必須包含string類標頭檔
#include<string>#include<iostream>using namespace std;main (){   string str;   getline(cin,str);   cout<<str<<endl;}

總結:
1.getline(cin,str)最好使用在標準string的C++代碼中,不要與char混合使用,導致getline();參數不匹配。
2.對於不管是C/C++程式中,盡量使用cin.getline(m,接受字串個數n,結束字元可省略),這樣可以避免與char類型,string類參數不匹配。

char ch;//單個字元char m[20];//字串char m[20][30];//二維字串數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.