- EX/ Read all integers from stdin and echo them one per line
- Stop on bad input or EOF
Note, >>
is C
βs right bit-shift operator,
- Shifts
a
βs bits to the right by b positions - But, when the left hand side is an input stream, like
cin
, it is the input operator
The operator >>
, when used for input, has cin
(type istream) as its first operand and the data to be read in as its second (which could be of several different types).
- **But, what is the output (return value) of the operator? ***
- The return value is the stream itself (for example,
cin
) - This is why we can write:
cin
reads into x, populates it, then this operator returns cin
back
So, the above code becomes
cin
or istreams in general, can be implicitly cast to boolean values. cin
is true
if its fail bit
is not set and false
otherwise.
Something thats not that important now, but is nice to know, is that cin
is an object in the istream
class.
An even better way of re-writing the above:
Example: Read all integers from stdin
until EOF
, skip over bad input.
- *What if I want to read in white space? **
we can use the header \#include <iomanip>
. This header provides several io manipulators which can help us (listed below).
IO manipulator mutates the stream:
STRINGS
In C
arrays, (char * or char []
), terminated by the null
character \\0
- Must explicitly manage memory - allocate more memory as string grows
- Easy to actually overwrite null terminator and corrupt memory
- Thankfully, we have built in type
string
inC++
. - we need include header:
std::string
- grows as needed (no need to manage memory)
- safer to manipulate
- In the above code snippet, even in C++, the literal
"hello"
is still a c-style string- i.e. a null-terminated array of characters
- The variable s is a string whose value has been initialized using that c-string
Perks of C++ Strings
- equality:
s1 == s2
- inequality:
s1 != s2
- comparison:
s1 <= s2
ands1 >= s2
- length:
s1.length()
length of a string - fetch individual chars:
s1[0]
etc⦠- concatenation:
s3 = s1+s2, s3+=s4
One way to read ws:getline(cin, s)
reads to the newline character into s
The stream abstraction applies to other data sources
- e.g. Files: read from a file instead of
stdin
:- In
C++
, we usestd::ifstream
to read from a file andstd::ofstream
to write to a file
- In
File access in C
File access in C++
Anything you can do with cin
and cout
, you can do with an ifstream
and ofstream
, respectively.
Example: The stream abstraction also applies to strings!
Types std::istring
stream for reading from a string
, std::ostring
stream for writing to a string
.
e.g. convert a string to a number
What is the stringstream
? Just like how cin
and cout
are read in from the iostream
, stringstream
makes a string just like a stream. That this, we can sort of imagine that string as an βinputβ and read from it. This could be useful when we want
to extract strings within a string or extract integers from a string
Basic methods are:
clear()
To clear the stream.str()
To get and set string object whose content is present in the stream.- operator
<<
- Add a string to thestringstream
object. - operator
>>
- Read something from thestringstream
object.
END OF LECTURE 5