Recently in doing pen test, compared with Leetcode, written questions are to write their own input and output, each time here are wasted a lot of time, this article summarizes how to input data into the array in C + +.
1. Enter the array size first, then enter data data, separated by a space or ' \ n ' character
1 /*2 * Two types of input methods3 * 34 * 1 2 35 *6 * 37 * 18 * 29 * 3Ten * One */ Avector<int>Inputarray () { - intSize =0; -CIN >>size; thevector<int>inputs; - for(intI=0; i<size;i++){ - inttmp; -CIN >>tmp; + Inputs.push_back (TMP); - } + returninputs; A}
2, enter the ', ' character segmentation of the data, thinking of the input is saved as a string, and then converted to specific data
1 //1,2,3,4,52vector<int>InputArray2 () {3 stringinputs;4CIN >>inputs;5 //Split6vector<string>splits;7 intStart =0;8 for(size_t i=0; I<inputs.size (); i++){9 if(Inputs[i] = =','){TenSplits.push_back (Inputs.substr (start,i-start)); OneStart = i+1; A } - } - if(Start! = (int) Inputs.size ()) { the Splits.push_back (Inputs.substr (start)); - } -vector<int>Res; - for(strings:splits) { + Res.push_back (Std::stoi (s)); - } + returnRes; A}
3. Enter an infinite length of data, separated by a space between the data, enter the end
1 //Enter an array of infinite lengths 1 2 3 4 5 6 ...2vector<int>inputArray3 () {3vector<int>inputs;4 intA;5 Do{6CIN >>A;7 Inputs.push_back (a);8} while(GetChar ()! ='\ n');9 returninputs;Ten One}
4. Similar to Method 1, enter a two-dimensional array
1vector<vector<int>>Inputmatrix () {2 intM,n;3CIN >> M >>N;4vector<vector<int>> Matrix (m,vector<int> (n,0));5 for(intI=0; i<m;i++){6 for(intj=0; j<n;j++){7 intA;8CIN >>A;9MATRIX[I][J] =A;Ten } One } A returnMatrix; -}
Interview: C + + input data