參考:https://blog.csdn.net/u011486738/article/details/82082405
/*
1. cin>>
用法1: 最基本,也是最常用的用法,輸入一個數字
用法2: 結束一個字符串,遇“空格”、TAB/回車結束
2. cin.get()
用法1. cin.get(字符變量名)可以用來接收字符
char ch;
ch=cin.get();//=cin.get(ch)
用法2. cin.get(字符數組名,接收字符數目)用來接收一行字符串,可以接收空格
char a[20];
cin.get(a,20);//有些類似 getline ,可以輸入多個單詞,中間空格隔開
用法3. cin.get(無參數) 沒有參數主要用於捨棄輸入流中的 不需要的字符,或者捨棄回車,彌補cin.get(字符數組名,接收字符數目)的不足.
例如:
cin.get(a,5);
cin.get();
cin.get(b,4);
當輸入:
adfg
adfjh
輸出:a:
adfg
b: adf
3. cin.geline()//接收一個字符串,可以接收空格並輸出
char m[20];
cin.getline(m,5);//接受5個字符到m中,其中最後一個爲\0,所以只看到4個字符輸出
cin.getline() 實際上接受三個參數,cin.getline(接受字符串到m,接受個數5,結束字符)
//當第三個參數省略時,系統默認爲\0 是 /n 換行符
cin.getline(m,5,'a') 當輸入: jkladfgh 輸出:jkl 當輸入:qwertyu 輸出:qwer
當在多維數組中的時候,也可以用cin.getline(m[i],20)之類的用法:
char m[3][20];
for(int i=0;i<3;i++)
{
cin.getline(m[i],20);
}
4. getline()//接受一個字符串,可以接收空格並輸出,需包含#include<string>
string str;
getline(cin,str)
cout<<str<<endl;
在string 類中,可以用str[i]來取str字符串中的第i個字符,同時也可以用char *p指向 &str[i]來取其字符。這裏的[]是string的操作符重載,而用指針string *p=&str;則是指向str;訪問裏面的某個字符時,只能(*p)[i];
cin.getline()類似,但是cin.getline()是屬於istream流,而getline()屬於string流,是不一樣的兩個函數
5. gets() //接受一個字符串,可以接收空格並輸出,需包含<string>
char m[20]
gets(m);
6. getchar()//接受一個字符,需包含<string>
ch=getchar()//但不能寫成getchar(ch) 是c語言函數,c++也兼容
*/
#include<iostream>
#include<string>
using namespace std;
int main()
{
char a, b,a1,b1;
string s2;
char s1[20],s3[10],s4[10];
char *p1;
gets(s1);
getline(cin, s2);
cin.getline(s1,1);
p1 = &s2[0]; // [] 這個是重構操作符出來的,不能像數組指針一樣的轉換使用
string *p2=&s2;
//p2 = s2;
cout << "S1:" << s1[2] << endl;
cout << "p1: "<<*p1 << endl;
cout << "p1: " << *(p1+1) << endl;
cout << "p2:" << *p2 << endl;
cout << "p22:" << (*p2)[1] << endl;
cout << "p23:" << (*p2)[0] << endl;
cout << "p24:" << p2+2<< endl; //*(p2+2) 有內存溢出錯誤,因爲p2存的s2地址,指向的是s2,*p則是s2; p2+2;是p2+sizeof(s2)
cout << "p25:" << &p2[2] << endl; // p2[2]=>*(p2+2)是一樣的道理;
//cout << "p25:" << p2[2] << endl;//erro
int c;
cin >>a >> b;
a1 = cin.get();
cin.get(b1);
cin.get(s3,5);
cin.getline(s4, 10, 'a');
c = a + b;
printf("%d", c);
//cout <<c<< endl;
system("pause");
return 0;
}