Skip to content

Commit 9c088e0

Browse files
committed
sync
1 parent 1155e8d commit 9c088e0

7 files changed

Lines changed: 376 additions & 96 deletions

File tree

‎cpp/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
1. [valarray](valarray.cpp)
6262
1. [deque](deque.cpp)
6363
1. [string](string.cpp)
64+
1. IO
65+
1. [sstream](sstream.cpp)
66+
1. [iostream](iostream.cpp)
6467
1. [thread](thread.cpp)
6568
1. [sleep_for](sleep_for.cpp)
6669
1. [atomic](atomic.cpp)

‎cpp/iostream.cpp‎

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

‎cpp/main.cpp‎

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -286,67 +286,6 @@ int main() {
286286
287287
*/
288288
{
289-
/*
290-
# cout
291-
292-
stdout.
293-
294-
`ostream` object.
295-
296-
For tests, std::stringstream shall be used as the results can then be tested,
297-
and the behaviour is identical to cout.
298-
299-
`<<` is very magic. You need to understand:
300-
301-
- operator overload
302-
- function template argument deduction
303-
- namespaces adl
304-
305-
before really understanding why it works.
306-
307-
# cerr
308-
309-
Cout for stderr
310-
311-
# clog
312-
313-
By default also points to stderr, but can be redirected with TODO.
314-
*/
315-
{
316-
std::cout << "cout" << std::endl;
317-
std::cerr << "cerr" << std::endl;
318-
std::clog << "clog" << std::endl;
319-
}
320-
321-
/*
322-
# cin
323-
324-
# stdin
325-
326-
`istream` object.
327-
328-
Avoid using it for similar reasons as scanf:
329-
330-
- hard to handle invalid inputs
331-
- difficult to predict behaviour
332-
333-
getline is the best option.
334-
*/
335-
{
336-
//std::cout << "Enter an integer:" << endl;
337-
//std::cin >> i;
338-
//std::cout << i << std::endl;
339-
}
340-
341-
// This is how a very explicit usage of `<<` would look like
342-
{
343-
std::stringstream ss;
344-
345-
//TODO0 how to get his working?
346-
//std::operator<<<std::ostream,std::string>(ss, "explicit");
347-
348-
std::operator<<(std::operator<<(ss, "explicit "), "call");
349-
}
350289

351290
// #<< left shift overload
352291
{

‎cpp/sstream.cpp‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include "common.hpp"
2+
3+
int main() {
4+
/*
5+
# stringstream
6+
7+
# basic_stringstream
8+
9+
http://en.cppreference.com/w/cpp/io/basic_stringstream
10+
11+
An iostream String backed implementation.
12+
13+
The following is defined:
14+
15+
typedef basic_stringstream<char> stringstream;
16+
17+
Very useful to test streams without creating files / stdin.
18+
19+
# ostringstream
20+
21+
TODO vs stringstream.
22+
23+
http://stackoverflow.com/questions/3292107/whats-the-difference-between-istringstream-ostringstream-and-stringstream-w
24+
*/
25+
{
26+
// Initialize
27+
{
28+
{
29+
std::stringstream ss("");
30+
ss << "ab";
31+
ss << "cd";
32+
assert(ss.str() == "abcd");
33+
}
34+
35+
// empty constructor: TODO everything fails to compile. Why?
36+
{
37+
std::stringstream ss();
38+
//ss << "ab";
39+
//ss << "cd";
40+
//assert(ss.str() == "abcd");
41+
}
42+
43+
// Nope, works like a file that has come content.
44+
// http://stackoverflow.com/questions/3840531/what-is-the-purpose-of-ostringstreams-string-constructor
45+
{
46+
std::stringstream ss("ab");
47+
ss << "cd";
48+
assert(ss.str() == "cd");
49+
}
50+
51+
// ostringstream + ate
52+
{
53+
std::ostringstream ss("ab", std::ios::ate);
54+
ss << "cd";
55+
assert(ss.str() == "abcd");
56+
}
57+
58+
// stringstream + ate: TODO fails, why?
59+
{
60+
std::stringstream ss("ab", std::ios::ate);
61+
ss << "cd";
62+
assert(ss.str() == "ab");
63+
}
64+
}
65+
66+
// Clear
67+
{
68+
std::stringstream ss("");
69+
// Clear stringstream
70+
// http://stackoverflow.com/questions/20731/in-c-how-do-you-clear-a-stringstream-variable
71+
ss.str("");
72+
// Clear flags. Very important, not only for error indicators but also for end of stream.
73+
ss.clear();
74+
assert(ss.str() == "");
75+
76+
// ERROR: use of deleted function because the constructor is =delete.
77+
//ss = std::stringstream();
78+
}
79+
}
80+
81+
}

‎cpp/string.cpp‎

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -217,38 +217,6 @@ int main() {
217217
}
218218
}
219219

220-
/*
221-
# stringstream
222-
223-
# basic_stringstream
224-
225-
An iostream String backed implementation.
226-
227-
The following is defined:
228-
229-
typedef basic_stringstream<char> stringstream;
230-
231-
typedef basic_stringstream<char>
232-
233-
Very useful to test streams without creating files / stdin.
234-
*/
235-
{
236-
std::stringstream oss("abcd");
237-
238-
// str does not clear the std::stringstream object
239-
assert(oss.str() == "abcd");
240-
241-
// To clear it you could do: http://stackoverflow.com/questions/20731/in-c-how-do-you-clear-a-stringstream-variable
242-
// Set to empty:
243-
oss.str("");
244-
// Clear flags. Very important, not only for error indicators but also for end of stream.
245-
oss.clear();
246-
assert(oss.str() == "");
247-
248-
// ERROR: use of deleted function because the constructor is =delete.
249-
//oss = std::stringstream();
250-
}
251-
252220
// Possible application: build up a huge string step by step.
253221
// May be more efficient than concatenations which always generates new objects.
254222
{
@@ -263,7 +231,7 @@ int main() {
263231
264232
There are a few standard alternatives.
265233
266-
<http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c>
234+
http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c
267235
*/
268236
{
269237
/*
@@ -299,8 +267,11 @@ int main() {
299267
}
300268
}
301269

302-
// #int to string
303-
// http://stackoverflow.com/questions/7663709/convert-string-to-int-c
270+
/*
271+
# string to int
272+
273+
http://stackoverflow.com/questions/7663709/convert-string-to-int-c
274+
*/
304275
{
305276
// Best C++11 error checking option: stoi
306277
#if __cplusplus >= 201103L

‎sdl/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1. [Animation](animation.c)
1111
1. [Animation user control](animation_user_control.c)
1212
1. [Animation user control discrete](animation_user_control_discrete.c)
13+
1. [Two player discrete](two_player_discrete.c)
1314
1. [Animation random walk](animation_random_walk.c)
1415
1. [Plane wave](plane_wave.c)
1516
1. [TTF](ttf.c)

0 commit comments

Comments
 (0)