-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathiostream.cpp
80 lines (57 loc) · 1.75 KB
/
iostream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "common.hpp"
int main() {
/*
# cout
stdout.
`ostream` object.
For tests, std::stringstream shall be used as the results can then be tested,
and the behaviour is identical to cout.
`<<` is very magic. You need to understand:
- operator overload
- function template argument deduction
- namespaces adl
before really understanding why it works.
# cerr
Cout for stderr
# clog
By default also points to stderr, but can be redirected with TODO.
*/
{
std::cout << "cout" << std::endl;
std::cerr << "cerr" << std::endl;
std::clog << "clog" << std::endl;
}
/*
# cin
# stdin
`istream` object.
Avoid using it for similar reasons as scanf:
- hard to handle invalid inputs
- difficult to predict behaviour
getline is the best option.
*/
{
//std::cout << "Enter an integer:" << endl;
//std::cin >> i;
//std::cout << i << std::endl;
}
// This is how a very explicit usage of `<<` would look like
{
std::stringstream ss;
//TODO0 how to get his working?
//std::operator<<<std::ostream,std::string>(ss, "explicit");
std::operator<<(std::operator<<(ss, "explicit "), "call");
}
/*
# cout unsigned char
Prints literal bytes.
http://stackoverflow.com/questions/19562103/uint8-t-cant-be-printed-with-cout/19562163#19562163
*/
{
std::cout << (unsigned char)1 << std::endl;
std::cout << (char)1 << std::endl;
std::cout << (signed char)1 << std::endl;
std::cout << (int)1 << std::endl;
std::cout << (unsigned int)1 << std::endl;
}
}