0

When looking at some code online I found

cin>>arr[0][0]>>arr[0][1]>>arr[0][2]

where I put a line of three integer values separated by space. I see that those three integers separated by space become the value of arr[0][0], arr[0][1] and arr[0][2].

It doesn't cause any trouble if there are more than one space between them.

plz, can anyone explain me how this work?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Anklon
  • 618
  • 2
  • 8
  • 21

2 Answers2

1

Most overloads of operator>> consume and discard all whitespace characters first thing. They begin parsing the actual value (say, an int) starting from the first non-whitespace character in the stream.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
0

Reading almost any types of inputs from a stream will skip any leading whitespaces first, unless you explicitly turn that feature off. You should read std::basic_istream documentation for more information:

Extracts an integer value potentially skipping preceding whitespace. The value is stored to a given reference value.

This function behaves as a FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts an integer value by calling std::num_get::get().

The same applies to other stream input functions, including the scanf family where most format specifiers will consume all whitespace characters before reading the value:

All conversion specifiers other than [, c, and n consume and discard all leading whitespace characters (determined as if by calling isspace) before attempting to parse the input. These consumed characters do not count towards the specified maximum field width.

Community
  • 1
  • 1
phuclv
  • 37,963
  • 15
  • 156
  • 475