All of the following programs default to main Function , Omit other statements
(1) cin When reading data from the input buffer , Before the first valid character is skipped [ Space ][Tab][ Line break ] These separators .
char a; cout<<"input:"; cin>>a; cout<<"char:"<<a <<endl;
At the end of [ enter ] Represents the end of the last input , The same below , Don't prompt again
input :[ Space ][Tab][ enter ]a[ enter ]

(2) cin After the character is read successfully, the separator after the character will remain in the input buffer .
Introduce a function :istream& getline (istream& is, string& str);
Extracts characters from is and stores them into str until the newline
character, ‘\n’.
For capture [ Line break ] symbol , And save all the contents before the newline character in the str in , And will ’\n’ Delete directly from the input buffer .
char a; string test; cin>>a; getline(cin,test);// No blocking cout<<a<<endl;
cout<<"test:"<<test<<endl;
input :a[ enter ]
This is the last one [ enter ] cover getline() to intercept , The input buffer is empty , So the direct output is empty .

input :a[ Space ]test[ enter ]
find [ Space ] Also stored in test in , It's printed out .

(3) cin.get() Gets the first character of the input buffer , No separator is ignored . After successful reading, the data after the character continues to remain in the input buffer .
char a,b; string str; cout<<"input:"; a=cin.get(); cin.get(b);
getline(cin,str); cout<<"a:"<<a <<endl; cout<<"b:"<<b <<endl; cout<<"str:"<<str
<<endl;
input :a[ enter ][ Space ][ Space ]dd[ enter ]
find b One more blank line is output , This is because of the second character entered [ enter ] Deposited by b Print out , Then print the last one endl. remainder [ Space ][ Space ]dd Has been deposited str Print out .

(4) use cin.get ( char* s, int len ) Read a row of data ,n Is the size of the target space .
char a; char p[10]={}; cin.get(p,10); cin.get(a); cout<<"a:"<<a <<endl;
cout<<"strlen(p):"<<strlen(p) <<endl;
input :a[ Space ]b[ Space ]c
cin.get End reading data when a carriage return is encountered , take ’\n’ The previous content is stored in the target space , incorrect ’\n’ Do anything . after ’\n’ cover a obtain .

(5) stay while() Input data in the loop ,ctrl+d Can only be used to end acceptance char Type data .
vector<char> v; char c; cout << "inout:"; while (cin>>c) v.push_back (c);
cout << "v.size()= " << int(v.size()) << endl; for(auto & x:v) cout<<x<<" ";


In the face of int Type data ,ctrl+d It doesn't work . Input can only be terminated by entering a non numeric character . Because non numeric characters are not int Type data , therefore while() It is judged that false, Jump out of the loop .
vector<int> v; int c; cout << "inout:"; while (cin>>c) v.push_back (c); cout
<< "v.size()= " << int(v.size()) << endl; for(auto & x:v) cout<<x<<" ";

(6) stay while() Two consecutive pairs in a loop vector< int > input data . In the first one while(cin>>tmpVal) After use
cin.clear() To clear flase state , Combined use cin.ignore() To ignore this invalid character . In the second while(cin>>tmpVal) To continue to accept input .
vector<int> a; vector<int> b; int tmpVal; cout<<"input A:";
while(cin>>tmpVal) a.push_back(tmpVal); cin.clear(); // Clear error status cin.ignore();
// Skip invalid data cout<<"input B:"; while(cin>>tmpVal) b.push_back(tmpVal); cin.clear();
cin.ignore(); cout<<"output A:"; for(auto & x:a) cout<<x<<" "; cout<<endl;
cout<<"output B:"; for(auto & x:b) cout<<x<<" "; cout<<endl;

Technology
©2020 ioDraw All rights reserved
Feature Engineering Comparison of data extraction tools :Kettle,Datax,Sqoop,StreamSets Nikkei : Sony and Jiaxia are actively applying for Huawei's supply license Self made whole person computer program k8s delete pod2020 Nobel Prize in physiology or medicine announced Interview questions : Handwritten list ( Include reverse linked list ) Some views of a student on Hongmeng system Children programming children's learning route Programmer's promotion 28 Sorting code of Guan 3