Premise
To be more exact, we should include all the needed headers first.
For example:
|
|
Actually, since we are usually hosted on machines with GCC, we can include only:
|
|
Which makes everything from the STL available. Remember this header is not standard.
Reading an array of N elements
The first line contains N
, the number of elements.
The next line contains N
space-separated integers.
|
|
Using copy_n
is the best choice because we can use it to read more things in sequence:
|
|
Reading strings
We can simply use std::string
:
|
|
If the string length is introduced first, we can either read it:
|
|
or ignore it (there are many ways, here is the simplest):
|
|
Output
We can easily print values and strings by sending them to cout
:
|
|
Printing arrays
Vectors and arrays are easy to std::copy
to standard output.
If v
is a vector or array of int
s, we can print its content separated by whitespaces this way:
|
|
If we must omit the last whitespace, we can do the following refinement:
|
|
Floating point with exact precision
Some challenges require printing floting points with a certain width.
In such cases, we use std::fixed
in combination with std::setprecision(numberOfDigits)
can be used.
For instance, suppose we want only two digits after comma:
|
|
Or suppose we want only one digit after comma:
|
|