C + + Instance input multiline number to array _c language

Source: Internet
Author: User

C + + input multiple lines of numbers to an array

The day before yesterday to do a company pen test, its input format is a number of lines, each with a space separator, in line with the end of the symbol input to more than one array. In Java, there are corresponding functions to split a row of groups directly, feeling in C + + This input method is still quite strange, today come up with a solution.

Ideas:

Each time a character is read, the judge is not EOF, and if it is, jump out of the loop;

Not EOF to put the character back into the buffer;

Read the line break, the end of line, processing array;

Read the character, if not a space, into a temporary string;

If it is a space, the string is converted into an integer and pressed into the array;

Attention:

For continuous space input to be judged, or you will enter a heap of 0;

The last temporary string is stored in the array when the line break is read;

Code:

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;

int main () {
  char flag;
  while ((Flag=getchar ())!=eof) {
    putchar (flag);
    string tmpstr;
    vector<int> Buff;
    char c;
    while ((c = GetChar ())!= ' \ n ') {
      if (c!= ')
        Tmpstr.push_back (c);
      else {
        if (tmpstr!= "") {
          buff.push_back (atoi (Tmpstr.c_str ()));
          Tmpstr = "";
    }}} if (tmpstr!= "")
      Buff.push_back (Atoi (Tmpstr.c_str ()));
    for (auto A:buff)
      cout << a << ";
    cout << ' \ n ';
  }
}


 

Thank you for reading, I hope to help you, thank you for your support for this site!

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.