Interview: C + + input data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.