Reading input to a dynamic-size array

What I'm trying to do is read a line from stdin and break it, using spaces as delimiters.

Say I have this as an input:

2
1 2
3 4

The first line gives me the number of lines I would like to read, these are all lines with integers separated by an unknown number of spaces (i.e. there can be 1 space, but it can also be 10 spaces). <w> What I was trying to do was read these lines into arrays with dynamic integer size.

It was very simple in Python:

foo = raw_input()
array = foo.split()

or even shorter:

foo = raw_input().split()

However, due to circumstances, I have to find out the beauty of C ++. So I tried to create something similar to the above Python code:

#include <iostream>

using namespace std;

int lines;
int *array;

int main() {
    cin >> lines;
    for (int line = 0; line < lines; line++) {
        // Something.
    }
}

, . , std:: cin , . , -, ...

, . .

+3
2

, , , .

std::getline() - std::string.

std::istringstream, , . ints

:

std::string line;
if(std::getline(std::cin, line))
{
  std::istringstream str(line);
  int lc;

  if (str >> lc) // now you have the line count..
  {
    // now use the same technique above
  }
}

oh " " std::vector<>

+1

++ [], , . cin , for , , . , , .

+1

All Articles